Skip to content

Instantly share code, notes, and snippets.

@yalin
Last active March 13, 2021 14:29
Show Gist options
  • Save yalin/6ba54e5e6628034e461e5088aa989113 to your computer and use it in GitHub Desktop.
Save yalin/6ba54e5e6628034e461e5088aa989113 to your computer and use it in GitHub Desktop.
Python argparser init
# argprase library that will help us to deal with arguments
import argparse
import sys
myParser = argparse.ArgumentParser(description='Definition of the python file')
myParser.add_argument('mandatory',
type=str,
help='Mandatory argument')
myParser.add_argument('--optional', '-o',
metavar='str',
type=str,
help='Optional argument')
myParser.add_argument('--default', '-d',
default='defaultValue',
help='Argument with default value')
myParser.add_argument('--list', '-l',
metavar='element',
nargs='+',
help='Argument list')
requiredArgs = myParser.add_argument_group('required arguments') # a new argument group added
requiredArgs.add_argument('--required', '-r',
type=str,
help='Required argument',
required=True)
argsList = myParser.parse_args()
print("argsList :", argsList)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment