# \--advanced-help for argparse

**URL:** <https://discuss.python.org/t/advanced-help-for-argparse/20319>\
**Category:** Ideas\
**Tags:** help\
**Created:** [October 25, 2022, 2:17pm UTC](https://discuss.python.org/t/advanced-help-for-argparse/20319 "2022-10-25T14:17:55Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![kenyee](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/kenyee/32/9497_2.png) [@kenyee](https://discuss.python.org/u/kenyee)\
**Post date:** [October 25, 2022, 2:17pm UTC](https://discuss.python.org/t/advanced-help-for-argparse/20319/1 "2022-10-25T14:17:55Z")

</div>

When using argparse, users can get a list of parameters by passing the -h or --help parameter.  
Sometimes, it’d be nice to hide debug parameters or rare usage parameters from the list of help and only show these w/ an --advanced-help parameter.

---

<div class="post-metadata">

**Author:** ![jeanas](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jeanas/32/8115_2.png) [@jeanas](https://discuss.python.org/u/jeanas)\
**Post date:** [October 25, 2022, 7:17pm UTC](https://discuss.python.org/t/advanced-help-for-argparse/20319/2 "2022-10-25T19:17:47Z")

</div>

Does this really need special support in `argparse` though? In such a case, I would do

```auto
import argparse

def make_parser(advanced_help=False):
    def advanced(description):
        return description if advanced_help else argparse.SUPPRESS

    p = argparse.ArgumentParser(prog="foo", description="Program with advanced options")
    p.add_argument("-s", "--simple-arg", metavar="VALUE", help="simple argument")
    p.add_argument(
        "-a", "--advanced-arg", help=advanced("advanced option that does this and that")
    )
    p.add_argument("-H", "--advanced-help", action="store_true", help="show the full list of options")
    return p

args = make_parser().parse_args()
if args.advanced_help:
    make_parser(advanced_help=True).print_help()
    raise SystemExit

```

---

<div class="post-metadata">

**Author:** ![BowlOfRed](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/bowlofred/32/3379_2.png) [@BowlOfRed](https://discuss.python.org/u/BowlOfRed)\
**Post date:** [October 25, 2022, 7:54pm UTC](https://discuss.python.org/t/advanced-help-for-argparse/20319/3 "2022-10-25T19:54:18Z")

</div>

From [argparse — Parser for command-line options, arguments and sub-commands — Python 3.8.17 documentation](https://docs.python.org/3.8/library/argparse.html?highlight=argparse#help)

> [`argparse`](https://docs.python.org/3.8/library/argparse.html?highlight=argparse#module-argparse) supports silencing the help entry for certain options, by setting the `help` value to `argparse.SUPPRESS`:
> 
> ```python
> >>> parser = argparse.ArgumentParser(prog='frobble')
> >>> parser.add_argument('--foo', help=argparse.SUPPRESS)
> >>> parser.print_help()
> usage: frobble [-h]
> 
> optional arguments:
> -h, --help show this help message and exit
> 
> ```

Not the greatest for having an advanced help, but okay if you just want a hidden option.

---

<div class="post-metadata">

**Author:** ![kenyee](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/kenyee/32/9497_2.png) [@kenyee](https://discuss.python.org/u/kenyee)\
**Post date:** [October 25, 2022, 8:15pm UTC](https://discuss.python.org/t/advanced-help-for-argparse/20319/4 "2022-10-25T20:15:21Z")

</div>

We actually do something like this…basically prefix a magic string in front of help which is then used to indicate to replace the help string with SUPPRESS just before print\_help() is called.  
Without it being part of argparse, you have fun edge cases like this though: [mutually exclusive group w/ all parameters argparse.SUPPRESS'd causes an assert · Issue #98666 · python/cpython · GitHub](https://github.com/python/cpython/issues/98666)

---

<div class="post-metadata">

**Author:** ![jeanas](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jeanas/32/8115_2.png) [@jeanas](https://discuss.python.org/u/jeanas)\
**Post date:** [October 25, 2022, 8:29pm UTC](https://discuss.python.org/t/advanced-help-for-argparse/20319/5 "2022-10-25T20:29:01Z")

</div>

That sounds like argparse.SUPPRESS just has a bug that should be fixed. I don’t see how this bug strengthens the case for adding your requested functionality to argparse. (To the contrary actually: if this bug is only noticed now, there must not be a ton of people who use argparse.SUPPRESS.)

---

<div class="post-metadata">

**Author:** ![vbrozik](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/vbrozik/32/7423_2.png) [@vbrozik](https://discuss.python.org/u/vbrozik)\
**Post date:** [October 25, 2022, 9:04pm UTC](https://discuss.python.org/t/advanced-help-for-argparse/20319/6 "2022-10-25T21:04:18Z")

</div>

When I need that some arguments change the way how other arguments are parsed, I first parse the deciding arguments then I parse the rest:

```python
import argparse

def parse_arguments(args=None):
    deciding_args_parser = argparse.ArgumentParser(add_help=False)
    deciding_args_parser.add_argument(
            '--advanced-help', required=False, action='store_true',
            help='show the full list of options')
    deciding_args, _ = deciding_args_parser.parse_known_args(args)

    parser = argparse.ArgumentParser(
            description='This is a demonstration of argument parser.',
            parents=[deciding_args_parser])
    parser.add_argument('-b', '--basic-option', help='Just a basic option.')
    parser.add_argument(
            '-a', '--advanced-option',
            help=('Advanced option!' if deciding_args.advanced_help
                else argparse.SUPPRESS))
    return parser.parse_args(
            args + (['-h'] if deciding_args.advanced_help else []))

```

* * *

```python
parse_arguments(['-h'])

```

```nohighlight
usage: ipykernel_launcher.py [-h] [--advanced-help] [-b BASIC_OPTION]

This is a demonstration of argument parser.

options:
  -h, --help show this help message and exit
  --advanced-help show the full list of options
  -b BASIC_OPTION, --basic-option BASIC_OPTION
                        Just a basic option.

```

* * *

```python
parse_arguments(['--advanced-help'])

```

```nohighlight
usage: ipykernel_launcher.py [-h] [--advanced-help] [-b BASIC_OPTION]
                             [-a ADVANCED_OPTION]

This is a demonstration of argument parser.

options:
  -h, --help show this help message and exit
  --advanced-help show the full list of options
  -b BASIC_OPTION, --basic-option BASIC_OPTION
                        Just a basic option.
  -a ADVANCED_OPTION, --advanced-option ADVANCED_OPTION
                        Advanced option!

```

---

<div class="post-metadata">

**Author:** ![kenyee](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/kenyee/32/9497_2.png) [@kenyee](https://discuss.python.org/u/kenyee)\
**Post date:** [October 25, 2022, 9:15pm UTC](https://discuss.python.org/t/advanced-help-for-argparse/20319/7 "2022-10-25T21:15:14Z")

</div>

hmm…true. Does sound like folks are doing this different ways…the root parser idea sounds reasonable. Might be worth a docs update?

---

<div class="post-metadata">

**Author:** ![eldipa](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/eldipa/32/8515_2.png) [@eldipa](https://discuss.python.org/u/eldipa)\
**Post date:** [October 27, 2022, 2:05pm UTC](https://discuss.python.org/t/advanced-help-for-argparse/20319/8 "2022-10-27T14:05:22Z")

</div>

I had the same use case than you. What I did was to [group all the advanced options](https://github.com/byexamples/byexample/blob/master/byexample/cmdline.py#L708-L720) under the same group, adding a `help` message as usual.

Then I crafted a special [HelpAction](https://github.com/byexamples/byexample/blob/master/byexample/cmdline.py#L312-L317) to hide or not the help of the advanced options.  
I [set this action](https://github.com/byexamples/byexample/blob/master/byexample/cmdline.py#L691-L706) for `-h` and for `-xh` (the latter is for show the advanced options)

So when the user pass `-h`, he/she will see all the non-advanced options./ When `-xh` is given, the full help is displayed.

In my case I also have a custom [HelpFormatter](https://github.com/byexamples/byexample/blob/master/byexample/cmdline.py#L238-L309) but probably you can resolve the hide of the advanced options without it.

---

<div class="post-metadata">

**Author:** ![kenyee](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/kenyee/32/9497_2.png) [@kenyee](https://discuss.python.org/u/kenyee)\
**Post date:** [October 27, 2022, 2:18pm UTC](https://discuss.python.org/t/advanced-help-for-argparse/20319/9 "2022-10-27T14:18:12Z")

</div>

We actually group functionality in different python files and then the main python file grabs all the parsers from the other python files. Sounds like we can pass an advanced option to the other parsers when this is done.

Also sounds like everyone is coming up w/ different ways to do the same concept 🙂
