Created
December 12, 2017 06:06
-
-
Save gyulkkajo/4e0145ad0e93b02311f3fd1876891e16 to your computer and use it in GitHub Desktop.
Python argparse module example and preset.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import argparse | |
if __name__ == '__main__': | |
desc = ''' | |
An example of how to use argparse | |
./CMD SUBCOMMAND [options...] | |
SUB COMMANDS: | |
record | |
report | |
... | |
''' | |
arg_parser = argparse.ArgumentParser(description=desc) | |
sub_parser = arg_parser.add_subparsers(dest='sub_cmd') | |
sub_init = sub_parser.add_parser('record', | |
help='Record') | |
sub_init.add_argument('-i', '--ip', | |
action='store', | |
required=True, | |
help='hostname for target') | |
sub_init.add_argument('--user', | |
action='store', | |
metavar='USER', | |
default='root', | |
help='User name for a target') | |
sub_init.add_argument('--port', | |
action='store', | |
type=int, | |
default=22, | |
help='Port for a target') | |
sub_init.add_argument('--append', | |
action='store_true', | |
help='Append traced data') | |
sub_report = sub_parser.add_parser('report', | |
help='Report helper') | |
sub_report.add_argument('--filter-pid', action='store', | |
nargs='*', | |
type=int, | |
metavar='PID', | |
help='Filter PIDs') | |
args = arg_parser.parse_args() | |
# If subcommand doesn't exist? | |
if not args.sub_cmd: | |
arg_parser.print_help() | |
exit(0) | |
if args.sub == 'record': | |
# Do something | |
pass | |
elif args.sub == 'report': | |
# Do something | |
pass | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment