Package: argparse_actions
This module implements some reusable custom actions to use with the argparse module.ExamplesThe following example, taken fromsamples/folder_actions.pydemonstrates the use of a custom action to verify the existence of a folder, specified from the command line:import argparse
import argparse_actions

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Custom Actions')
    parser.add_argument('directory',
                action=argparse_actions.FolderExistsAction)

    try:
        args = parser.parse_args()
        print 'Directory exists: {0}'.format(args.directory)
    except argparse_actions.NonFolderError as e:
        print 'Directory does not exist'
        print eIn the next example fromsamples/proper_ip.py, we use theProperIpFormatActioncustom action to verify if an IP address from command line is properly formatted:import argparse
import argparse_actions

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Custom Actions')
    parser.add_argument('ip',
                    action=argparse_actions.ProperIpFormatAction)

    try:
        args = parser.parse_args()
        print 'IP is properly formatted: {0}'.format(args.ip)
    except argparse_actions.InvalidIp as e:
        print 'IP is invalid: {0}'.format(e.ip)
        # This will display similar output:
        # print eExtending the Custom ActionsIf you find a custom action that almost do what you want, you canWrite your own from scratchSubmit an enhancement requestExtend the existing custom actionI am not commenting on option 1–it is your choice. For option 2, I will be gladly to accept any reasonable request, but sometimes life happens and I might not response quickly enough. That leaves you with the third option of extending the custom action yourself. Don’t worry, it is not that hard. In the next example, I will take theProperIpFormatActioncustom action and extend it to include ‘localhost’ as one of the proper IP format:import argparse
import argparse_actions

class IpAndLocalhostAction(argparse_actions.ProperIpFormatAction):
    def __call__(self, parser, namespace, values, option_string=None):
        # Do our check: allow for 'localhost'
        if values == 'localhost':
            setattr(namespace, self.dest, values)
        else:
            # Super class to perform its check
            parent = super(IpAndLocalhostAction, self)
            parent.__call__(parser, namespace, values, option_string)

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Custom Actions')
    parser.add_argument('ip', action=IpAndLocalhostAction)

    try:
        args = parser.parse_args()
        print 'IP is valid: {0}'.format(args.ip)
    except argparse_actions.InvalidIp as e:
        print eDiscussion:The first step is to create a new class (IpAndLocalhostAction), based on an existing custom action (argparse_actions.ProperIpFormatAction, which is really a class itself)Define the function__call__to override the base custom action with your own logic.More IdeasHere are a few ideas I have in mind, which I might implement:ExtendProperIpFormatActionto determine if and IP…Is reachableProvides some services such as HTTP or FTPBelongs to a particular list, such as the banned IP listExtendFolderExistsActionto determine if the folder is…WritableEmptyA symbolic link