Skip to content

argparse

jasper-zanjani edited this page Aug 6, 2020 · 1 revision

Define information that will appear when the user wants help. The string helptext contains the usage that will appear with -h or --help.

ArgumentParser(description=helptext)

Add a positional argument

  • positional arguments are valid if only one option is provided
  • do not require - at the beginning of the string
  • help string is what is printed next to the argument name in the help dialog
ArgumentParser.add_argument("x", type=int, help="the base")

Add a named parameters

  • - is required at the beginning of the string
  • values need to be passed after = or <space>
  • no longer behaves as a positional argument
ArgumentParser.add_argument('-r','--radius',type=int,required=True,help='radius)

Add a group of mutually exclusive arguments (may not be used together)
Usage appears as [-v | -q] indicating one of the options may be used. Many invocation options can be added to the same argument:

qv=ArgumentParser.add_mutually_exclusive_group()
qv.add_argument("-v","--verbose", action="store_true")
qv.add_argument("-q","--quiet","-s","--silent", action="store_true",help='quiet/silent mode')

Clone this wiki locally