Skip to content

Instantly share code, notes, and snippets.

@kirankotari
Created April 10, 2022 11:09
Show Gist options
  • Save kirankotari/c14929c3a2dbd894c09f1af18d5a6522 to your computer and use it in GitHub Desktop.
Save kirankotari/c14929c3a2dbd894c09f1af18d5a6522 to your computer and use it in GitHub Desktop.
import argparse
# Create the parser and add arguments
# parser = argparse.ArgumentParser()
# # 1) init arg
# parser.add_argument(dest='argument1', help="This is the first argument")
# # Parse and print the results
# args = parser.parse_args()
# print(args.argument1)
# # 2) arg types
# # Cast the input to string, int or float type
# parser.add_argument(dest='argument1', type=str, help="A string argument")
# parser.add_argument(dest='argument2', type=int, help="An integer argument")
# parser.add_argument(dest='argument3', type=float, help="A float argument")
# # Validate that the input is in specified list
# parser.add_argument(dest='argument4', choices=['red', 'green', 'blue'])
# # Accept multiple inputs for an argument, returned as a list
# # Will be of type string, unless specified
# parser.add_argument(dest='argument5', nargs=2, type=float)
# # Optional positional argument (length 0 or 1)
# parser.add_argument(dest='argument6', nargs='?')
# args = parser.parse_args()
# if args.argument1:
# print(f"arg1: {args.argument1}")
# if args.argument2:
# print(f"arg2: {args.argument2}")
# if args.argument3:
# print(f"arg3: {args.argument3}")
# if args.argument4:
# print(f"arg4: {args.argument4}")
# if args.argument5:
# print(f"arg5: {args.argument5}")
# if args.argument6:
# print(f"arg6: {args.argument6}")
# # 3) arg type few more
# # Boolean flag (does not accept input data), with default value
# parser.add_argument('-a1', action="store_true", default=False)
# # Cast input to integer, with a default value
# parser.add_argument('-a2', type=int, default=0)
# # Provide long form name as well (maps to 'argument3' not 'a3')
# parser.add_argument('-a3', '--argument3', type=str)
# # Make argument mandatory
# parser.add_argument('-a4', required=True)
# # Retur the input via different parameter name
# parser.add_argument('-a5', '--argument5', dest='my_argument')
# args = parser.parse_args()
# print(args.a1)
# print(args.a2)
# print(args.argument3)
# print(args.a4)
# print(args.my_argument)
# 4) arg grouping
# group = parser.add_mutually_exclusive_group()
# group.add_argument('--arg1', action='store_true')
# group.add_argument('--arg2', action='store_false')
# # 5) arg custom data type
# def single_word(string):
# # Check input does not contain spaces
# if (' ' in string):
# msg = f'\"{string}\" is not a single word'
# raise argparse.ArgumentTypeError(msg)
# return string
# parser.add_argument('argument1', type=single_word)
# args = parser.parse_args()
# print(args.argument1)
# parser.add_argument(dest='config', nargs='?')
# === Testing sublevels
# parent_parser = argparse.ArgumentParser(add_help=False)
# parent_parser.add_argument('--user', '-u',
# help='username')
# parent_parser.add_argument('--debug', default=False, required=False,
# action='store_true', dest="debug", help='debug flag')
# main_parser = argparse.ArgumentParser()
# service_subparsers = main_parser.add_subparsers(title="service",
# dest="service_command")
# service_parser = service_subparsers.add_parser("first", help="first",
# parents=[parent_parser])
# action_subparser = service_parser.add_subparsers(title="action",
# dest="action_command")
# action_parser = action_subparser.add_parser("second", help="second",
# parents=[parent_parser])
# args = main_parser.parse_args()
# # Needed for me
# parent_parser.add_argument('--debug', default=False, required=False,
# action='store_true', dest="debug", help='debug flag')
# parser.add_argument(dest='config', nargs='?')
# parser.add_argument('-a5', '--argument5', dest='my_argument')
"""
>>> parser = argparse.ArgumentParser(
... prog='PROG',
... formatter_class=argparse.ArgumentDefaultsHelpFormatter)
>>> parser.add_argument('--foo', type=int, default=42, help='FOO!')
>>> parser.add_argument('bar', nargs='*', default=[1, 2, 3], help='BAR!')
"""
parser = argparse.ArgumentParser(
prog='weblinks',
add_help=False
)
parser.add_argument("substring", help="sub-string filter")
parser.add_argument("-w", "--web", default=None, help="the website")
parser.add_argument("-u", "--username", default=None, help="web login username")
parser.add_argument("-p", "--password", default=None, help="web login password")
parser.add_argument("-e", "--ext", default=None, help="file extention")
parser.add_argument('-d', '--download', action='store_true', help="download links")
parser.add_argument("-v", "--verbosity", action="count", default=0)
parent_config_parser = argparse.ArgumentParser(
prog='weblinks',
add_help=False
)
parent_config_parser.add_argument('-w', '--web', nargs="?", default=None, help='web site url')
parent_config_parser.add_argument('-u', '--username', nargs="?", default=None, help='web site username')
parent_config_parser.add_argument("-e", "--ext", nargs="?", default=None, help="file extention")
parent_config_parser.add_argument('-d', '--download', action='store_true', help="download links")
parent_config_parser.add_argument('-s', '--show', default=False, action='store_true', help="display configuration")
config_main_parser = argparse.ArgumentParser(
prog='weblinks'
)
config_parsers = config_main_parser.add_subparsers(title="configuration", dest="config")
config_parsers.add_parser("substring", help="the sub-string in the links", parents=[parser])
config_parsers.add_parser("local", help="weblinks local configuration", parents=[parent_config_parser])
config_parsers.add_parser("global", help="weblinks global configuration", parents=[parent_config_parser])
args = config_main_parser.parse_args()
print(args)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment