Last active
June 22, 2025 23:17
-
-
Save thingsiplay/ae9a26322cd5830e52b036ab411afd1f to your computer and use it in GitHub Desktop.
Python Tutorial: argparse advanced-help with additional options
This file contains hidden or 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
#!/usr/bin/env python3 | |
import sys | |
import argparse | |
long_help = False | |
for arg in sys.argv: | |
if arg == "--": | |
break | |
if arg == "--long-help": | |
long_help = True | |
parser = argparse.ArgumentParser() | |
parser.add_argument( | |
"-f", | |
"--with-filename", | |
action="store_true", | |
default=False, | |
help="print for each playlist file the path to stderr", | |
) | |
parser.add_argument( | |
"-s", | |
"--sort", | |
action="append", | |
help="sort all game items by value of chosen key, before filter, *multi" | |
if long_help | |
else "sort all items", | |
) | |
parser.add_argument( | |
"-c", | |
"--count", | |
action="store_true", | |
default=False, | |
help="print only a count of matching items per list, output file unaffected" | |
if long_help | |
else argparse.SUPPRESS, | |
) | |
if long_help: | |
args = parser.parse_args(sys.argv[0:0] + ["--help"] + sys.argv[1:]) | |
else: | |
args = parser.parse_args() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What is this?
A way to handle a so called advanced help menu, where additional options are listed that are otherwise hidden with regular help. Hidden options should still function. This is just to have less clutter in normal view.
I've researched the web to see how people does it, and this is the way I like most so far. If you think this is problematic, please share your thoughts. This is for a commandline terminal application, that could also be automated through a script.
Before the
ArgumentParser()
is called, we check thesys.argv
for the trigger option--advanced-help
. Depending on this we set a variable to true or false. Then with the setup of the parser after theArgumenParser()
call, we add the--advanced-help
option to the list of regular help.Continue setting up your options as usual. But for the help description of those you want to exclude when using just regular
-h
, add an inline if else statement (ternary statement). This statement will put the help description only ifadvanced_help
variable is true, otherwise it putsargparse.SUPPRESS
to hide the option. Do this with all the options you want to hide.Or instead hiding the option with
argparse.SUPPRESS
, one could also just display a short option description instead. This way those displayed with-h
would have short explanation and when using the advanced help, it would display the long description.At last we need to actually parse what you just setup. For this we need to assign our custom list, that is based on the
sys.argv
, plus the regular--help
option. This way we can use--advanced-help
without the need for-h
or--help
in addition to show any help message.Run following program once with
./thing.py -h
and./thing.py --advanced-help
.