2

I was writing down a code in python which makes use of argparse module to make it easier to use it.

I can add arguments in the form of flags like:

parser.add_argument('-u', '--url', dest='url', help='type in url')

But like in gobuster, when you use help flag (-h) you can see you can also pass commands like dir,fuzz,etc. They also have separate help flags for each of them like

gobuster dir --help

how to achieve this? I can make flags without arguments by:

parser.add_argument('R', action='store_true', help='allow recursion')

In short, I'm trying to find out how to define commands separately so that they also show up under commands in help section and not under optional arguments. I'm also trying to find out how to create a separate help section for each command.

h4kr
  • 238
  • 1
  • 3
  • 13
  • Does the [official tutorial](https://docs.python.org/3/howto/argparse.html) help? – Karl Knechtel Dec 12 '21 at 08:06
  • @KarlKnechtel Yes, I looked through official page, but unfortunately I could not find the solution there. – h4kr Dec 12 '21 at 08:08
  • 1
    If I get what you want - have a look at [Click](https://click.palletsprojects.com/en/8.0.x/commands/). See also https://stackoverflow.com/q/59391084/4046632 – buran Dec 12 '21 at 08:11
  • 1
    Also with argparse - check https://stackoverflow.com/q/7498595/4046632 – buran Dec 12 '21 at 08:12
  • 3
    https://docs.python.org/3/library/argparse.html#sub-commands is one option. Another could be `argument_groups`, https://docs.python.org/3/library/argparse.html#argument-groups. Groups affect help display only, not parsing. "optional arguments" is just one default argument_group. You can make more with your own choice of names. – hpaulj Dec 12 '21 at 08:19
  • @buran, I suspect that link is to a more complicated subparsers usage than this user needs. – hpaulj Dec 12 '21 at 08:21
  • @hpaulj, I agree. But the fact is we all guess and it is not very clear what they really want. – buran Dec 12 '21 at 08:28

1 Answers1

1

It is possible to add it via subparsers with separate help message and parent arguments.

 arg_parser.add_argument('--version', action='version', version='%(prog)s 1.0')
 subparsers = arg_parser.add_subparsers(title='subcommands',dest='subcommandsarg',help='Program parser for subcommands')
 parser1 = argparse.ArgumentParser(add_help=False)
 parser1.add_argument('-a','--arg1', dest='module',required=False,help='Mod Name' ,default='all')
 subparsers.add_parser('parser1',parents=[parser1],help="Help text for parser1")
 parser2 = argparse.ArgumentParser(add_help=False)
 parser2.add_argument('-b','--arg2', dest='module',required=False,help='Mod Name' ,default='all')
 subparsers.add_parser('parser2',parents=[parser2],help="Gets the configuration of mods")
 options = arg_parser.parse_args()

output

#python test.py -h
usage: program [-h] [--version] {parser1,parser2} ...

optional arguments:
  -h, --help         show this help message and exit
  --version          show program's version number and exit

subcommands:
  {parser1,parser2}  Program parser for subcommands
    parser1          Help text for parser1
    parser2          Gets the configuration of mods

#python test.py parser1 -h
usage: program parser1 [-h] [-a MODULE]

optional arguments:
  -h, --help            show this help message and exit
  -a MODULE, --arg1 MODULE
                        Mod Name

#python test.py parser2 -h
usage: program parser2 [-h] [-b MODULE]

optional arguments:
  -h, --help            show this help message and exit
  -b MODULE, --arg2 MODULE
                        Mod Name