Python argparse: Difference between revisions
From wikinotes
Line 10: | Line 10: | ||
parser = argparse.ArgumentParser(description='Does thing real well') | parser = argparse.ArgumentParser(description='Does thing real well') | ||
parser.add_argument('-v', '--verbose', action='store_true', help='enable verbose logging') | parser.add_argument('-v', '--verbose', | ||
action='store_true', help='enable verbose logging') | |||
args = parser.parse_args() | args = parser.parse_args() | ||
print(args.verbose) # True | print(args.verbose) # True |
Revision as of 15:34, 9 April 2022
Argparse is a builting python library for building commandline interfaces.
TODO:
this page is just bad. clean me
Simple Example
import argparse parser = argparse.ArgumentParser(description='Does thing real well') parser.add_argument('-v', '--verbose', action='store_true', help='enable verbose logging') args = parser.parse_args() print(args.verbose) # True
argument types
Summary
# number of args (nothing) # positional nargs=1 # flag with N args nargs='+' # flag with one or more args (args.arg = []) nargs='?' # flag with one or no args (args.arg = None, 'var') #(set const=val to know when flag issued with no arguments) nargs='*' # flag with multiple or no args (args.arg = None, [], ['a','b',...]) # boolean args action='store_true' action='store_false'Examples
# positional parser.add_argument('output-file', help='filepath to save spreadsheet to') # flag with single argument parser.add_argument('-s', '--start-date', nargs=1, help='date to start from') # boolean arguments parser.add_argument('-v', '--verbose', action='store_true', help='verbose logging')
Argument Properties
parser.add_argument('-p','--filter-projects', default='monday', metavar='MON', # displayed in help as value type=str)
SubParsers
Sometimes a single CLI interface provides access to a handful of related classes. These classes may take entirely different parameters. In this case, you would implement a subparser for that set of commands.
parser = argparse.ArgumentParser( formatter_class = argparse.RawTextHelpFormatter, description = 'A collection of tools for operating on config files', ) subparsers = parser.add_subparsers( dest='subparser_name')## Subparser1 (ModifyKeyVals) ## modkeyval_parser = subparsers.add_parser( 'ModifyKeyVals', help='Flexible tool to modify configfiles that use the key=value format') modkeyval_parser.add_argument( '-f', '--filepath', help='Path to the configfile we are processing', metavar = '/etc/rc.conf', ) modkeyval_parser.add_argument( '-v', '--verbose', help='Enable detailed logging', metavar = '/etc/rc.conf', ) ## Subparser2 (SSHConfig) ## sshconfig_parser = subparsers.add_parser( 'SSHConfig', help='modify ~/.ssh/ssh_config "Host" entries. Expects to be run as the user whose ssh_config you are modifying.') sshconfig_parser.add_argument( '-hs', '--host', nargs='+', help='SSH Host value(s)', metavar = ['gitbox', '192.168.1.202'], )## Using Variables from SubParsers ## args = parser.parse_args() if args.subparser_name == 'ModifyKeyVals': filepath = args.filepath elif args.subparser_name == 'SSHConfig': host = args.host
python myscript.py ModifyKeyVals -f /path/to/file -v