Generate usage spec KDL files from Python argparse.ArgumentParser
Project description
argparse-usage
Generate usage KDL files from Python's argparse.ArgumentParser.
This library converts Python argparse definitions to the usage spec format, enabling automatic generation of:
- Markdown documentation
- Manpages
- Shell completions (bash, fish, zsh, powershell, nu)
Installation
pip install argparse-usage
# or
uv add argparse-usage
Usage
import argparse
import argparse_usage
# Create your ArgumentParser as usual
parser = argparse.ArgumentParser(
prog='mycli',
description='My CLI tool',
)
parser.add_argument('-v', '--verbose', action='count', default=0, help='Increase verbosity')
parser.add_argument('-f', '--force', action='store_true', help='Force operation')
parser.add_argument('files', nargs='+', help='Files to process')
# Add subcommands
subparsers = parser.add_subparsers(dest='command')
build_cmd = subparsers.add_parser('build', help='Build project')
test_cmd = subparsers.add_parser('test', help='Run tests')
# Generate usage spec
spec = argparse_usage.generate(
parser,
name='My CLI',
version='1.0.0',
author='Your Name',
)
print(spec)
Output:
// @generated by argparse-usage from Python argparse
name "My CLI"
bin "mycli"
version "1.0.0"
author "Your Name"
about "My CLI tool"
arg "<files>..." help="Files to process" var=#true var_min=1
flag "-v --verbose" help="Increase verbosity" default="0" count=#true
flag "-f --force" help="Force operation" default=#false
cmd build {
// options
}
cmd test {
// options
}
Generating Documentation
Save the spec and use the usage CLI to generate documentation:
# Save spec to file
python mycli.py --usage-spec > mycli.usage.kdl
# Generate markdown documentation
usage generate markdown --file mycli.usage.kdl --out-file README.md
# Generate shell completions
usage generate completion bash mycli --file mycli.usage.kdl > mycli-completion.bash
Supported Features
Flags (optional arguments)
| argparse action | usage spec mapping |
|---|---|
action='store_true' |
flag "--force" default=#false |
action='store_false' |
flag "--quiet" default=#true |
action='count' |
flag "-v --verbose" count=#true |
nargs='*' |
flag "--files..." var=#true |
nargs='+' |
flag "--tags..." var=#true var_min=1 |
choices=[...] |
flag "--format" { arg "<format>" { choices "json" "yaml" } } |
Arguments (positional)
| argparse nargs | usage spec mapping |
|---|---|
nargs=None (default) |
arg "<file>" |
nargs='?' |
arg "[file]" |
nargs='*' |
arg "[files]..." var=#true |
nargs='+' |
arg "<files>..." var=#true var_min=1 |
nargs=N |
arg "<coords>..." var_min=N var_max=N |
Parent Parsers
Inheritance from parent parsers is supported:
parent_parser = argparse.ArgumentParser(add_help=False)
parent_parser.add_argument('-v', '--verbose', action='count')
parser = argparse.ArgumentParser(parents=[parent_parser])
Subcommands
Nested subcommands are fully supported:
subparsers = parser.add_subparsers()
add_cmd = subparsers.add_parser('add')
add_cmd.add_argument('name')
Output:
cmd add {
arg "<name>"
}
API Reference
generate(parser, name=None, version=None, author=None, bin_name=None)
Generate a usage spec KDL string from an ArgumentParser.
Parameters:
parser(ArgumentParser): The ArgumentParser instance to convertname(str, optional): Friendly name for the CLI (defaults to parser.prog)version(str, optional): Version of the CLIauthor(str, optional): Author of the CLIbin_name(str, optional): Binary name (defaults to parser.prog)
Returns:
str: KDL-formatted usage spec string
Examples
See the examples/ directory for complete examples:
- basic_usage.py - Basic usage with flags, args, and subcommands
- complex_usage.py - Complex CLI with nested subcommands (config, database, deploy)
For information on setting up shell completions, see the examples README.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file argparse_usage-0.1.1.tar.gz.
File metadata
- Download URL: argparse_usage-0.1.1.tar.gz
- Upload date:
- Size: 5.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e143fcb38048e1ea331ca9abf4171fa06f7bae075cc7720029a0003016c189a1
|
|
| MD5 |
e398cd2875566b95677673947715448d
|
|
| BLAKE2b-256 |
2ec948d58c7c1bd72f75ccaa92ff9504ac25bacc84fb3f7a2dfacb03240d3937
|
Provenance
The following attestation bundles were made for argparse_usage-0.1.1.tar.gz:
Publisher:
release.yml on acidghost/argparse-usage
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
argparse_usage-0.1.1.tar.gz -
Subject digest:
e143fcb38048e1ea331ca9abf4171fa06f7bae075cc7720029a0003016c189a1 - Sigstore transparency entry: 1059647141
- Sigstore integration time:
-
Permalink:
acidghost/argparse-usage@fa4cac31b1ccb7cc3d41fc778415097f50d9aa24 -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/acidghost
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@fa4cac31b1ccb7cc3d41fc778415097f50d9aa24 -
Trigger Event:
push
-
Statement type:
File details
Details for the file argparse_usage-0.1.1-py3-none-any.whl.
File metadata
- Download URL: argparse_usage-0.1.1-py3-none-any.whl
- Upload date:
- Size: 7.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
eff437314084ecd9e4b099602da32136339f20f6e6259ffeaef2a706910cc319
|
|
| MD5 |
5a4a59990a0f239fcea158cf796178a2
|
|
| BLAKE2b-256 |
46ccf4e68943e03024916c8c5296112347b6323408c0d4105330c1865840017a
|
Provenance
The following attestation bundles were made for argparse_usage-0.1.1-py3-none-any.whl:
Publisher:
release.yml on acidghost/argparse-usage
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
argparse_usage-0.1.1-py3-none-any.whl -
Subject digest:
eff437314084ecd9e4b099602da32136339f20f6e6259ffeaef2a706910cc319 - Sigstore transparency entry: 1059647142
- Sigstore integration time:
-
Permalink:
acidghost/argparse-usage@fa4cac31b1ccb7cc3d41fc778415097f50d9aa24 -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/acidghost
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@fa4cac31b1ccb7cc3d41fc778415097f50d9aa24 -
Trigger Event:
push
-
Statement type: