> [!CAUTION] > Don't use this repo, the propper way to do it is to write a custom argparse.RawDescriptionHelpFormatter > See e.g. https://chromium.googlesource.com/catapult.git/+/HEAD/devil/devil/utils/markdown.py
Argdown is an extension to Python’s argparse module that adds Markdown and RST documentation export.
The argparse module is great at generating command-line usage and help texts,
but pasting pre-formatted and indented terminal output into a readme.md
is
quite ugly. argdown is a python module that provides a function
argdown.md_help(parser)
that accepts an ArgumentParser
object and
returns a string of lovely help text.
Argdown requires a fully formed ArgumentParser object, after all the
add_argument()s have been executed. The only way to make sure the
ArgumentParser object is created in the same way that it would be during normal
script execution is to execute the script until the arguments are parsed. To do
this, argdown reads the input file(s) until it reads a line containing
.parse_args(
. The rest of the file, being irrelevant to the command-line
invocation, is truncated, and a call to argdown.md_help()
is inserted to
generate the Markdown from the parser. It is important to note that this means
the whole script up until the call to parse_args
is executed in its
entirety, including any side-effects that may entail --- argdown does not
attempt to sanitize the code in any way.
usage: argdown [-h] [-] [--license] [--header HEADER] [--usage-header USAGE_HEADER] [--ref-header REF_HEADER] [--args-header ARGS_HEADER] [-s] [-r] [-e HIERARCHY] [-d] [-t] [--header-depth HEADER_DEPTH] [--encoding ENCODING] [-f FUNCTION] [-v] [src_file [src_file ...]]
Short | Long | Default | Description |
-h |
--help |
Show help | |
- |
Read from STDIN | ||
--license |
Print license | ||
--header |
Arguments and Usage |
Header text | |
--usage-header |
Usage |
Header text | |
--ref-header |
Quick reference table |
Header text | |
--args-header |
Arguments |
Header text | |
-s |
--spacey |
Blank lines after headers | |
-r |
--rst |
Generate rst | |
-e |
--hierarchy |
#=-*+. |
rst header order |
-d |
--hide-default |
Hide default arg values | |
-t |
--truncate-help |
Truncate help in this table | |
--header-depth |
1 |
Header depth of top header | |
--encoding |
utf-8 |
Input file encoding | |
-f |
--function |
None |
Function to call in file |
-v |
--version |
Show version |
show this help message and exit
Read from STDIN instead of a file.
Print license information (MIT) and exit.
Header text for the Arguments and Usage section.
Header text for the Usage section.
Header text for the Quick reference table section, a simple table of all the arguments.
Header text for the Arguments section, a detailed listing of all the arguments.
Output a blank line after headers.
Generate rst (reStructured Text) instead of Markdown.
Order of header characters to use for rst output.
Don't output default values for the arguments.
Truncate help in the Quick reference table section so that the table's width doesn't exceed --width. Makes terminal output prettier but means you'll probably have to re-write help messages.
Header depth; number of hashes to output before the top-level header.
Encoding of all input files. Frankly, there's no excuse to ever use this argument
Function to be called to parse args. For example, if the arg-parsing mechanism is contained in a console() function (common if the script is a module and has a console entry point defined), enter --function console if console() must be called to define the argument parser.
show program's version number and exit
If a file test.py
reads
import argparse import argdown parser = argparse.ArgumentParser(description='Process some integers.') parser.add_argument('integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator') parser.add_argument('--sum', dest='accumulate', action='store_const', const=sum, default=max, help='sum the integers (default: find the max)') args = parser.parse_args()
Then running
argdown test.py
Will output
# Arguments and Usage ## Usage ``` usage: argdown [-h] [--sum] N [N ...] ``` ## Arguments ### Quick reference table |Short|Long |Default |Description |-----|--------|-----------------------|---------------------------------------- |`-h` |`--help`| |show this help message and exit | |`--sum` |<built-in function max>|sum the integers (default: find the max) ### `-h`, `--help` show this help message and exit ### `--sum` (Default: <built-in function max>) sum the integers (default: find the max)
There are no known bugs.
Probably a lot. This script was built to handle the subset of argparse
’s
features that I use, so I imagine there are areas in which argdown
performs
poorly. Please open an issue if you find something.
The quick reference table output isn’t great; see above where the Default
column is included despite containing no content.
Currently, without truncate_help=False
passed to argdown.md_help
, the
description field at the end of the table is truncated to the width of the
terminal to prevent the table from looking awful. I’d like to add a feature to
pass a dict of short descriptions to improve that in the future.
MIT, see license.txt