Skip to main content

Yet another docopt, a human-friendly command line arguments parser.

Project description

     

YadOpt - Yet another docopt

YadOpt is a Python re-implementation of docopt and docopt-ng, a human-friendly command-line argument parser with type hinting and utility functions. YadOpt helps you creating beautiful command-line interfaces, just like docopt and docopt-ng. However, YadOpt also supports (1) date type hinting, (2) conversion to dictionaries and named tuples, and (3) save and load functions.

The following is the typical usage of YadOpt:

"""
Usage:
    train.py <config_path> [--epochs INT] [--model STR] [--lr FLT]
    train.py --help

Train a neural network model.

Arguments:
    config_path     Path to config file.

Training options:
    --epochs INT    The number of training epochs.   [default: 100]
    --model STR     Neural network model name.       [default: mlp]
    --lr FLT        Learning rate.                   [default: 1.0E-3]

Other options:
    -h, --help      Show this help message and exit.
"""

import yadopt

if __name__ == "__main__":
    args = yadopt.parse(__doc__)
    print(args)

Please save the above code as sample.py and run it as follows:

$ python3 sample.py config.toml --epochs 10 --model=cnn
YadOptArgs(config_path=config.toml, epochs=10, model=cnn, lr=0.001, help=False)

In the above code, the parsed command-line arguments are stored in the args variable, and you can access each argument using dot notation, like arg.config_path. Also, the parsed command-line arguments are typed, in other words, the args variable satisfies the following assertions:

assert isinstance(args.config_path, pathlib.Path)
assert isinstance(args.epochs, int)
assert isinstance(args.model, str)
assert isinstance(args.lr, float)
assert isinstance(args.help, bool)

More realistic examples can be found in the examples directory.

Installation

Please install from pip.

$ pip install yadopt

Usage

Use parse function

The yadopt.parse function allows you to parse command-line arguments based on your docstring. The function is designed to parse sys.argv by default, but you can explicitly specify the argument vector by using the second argument of the function, just like as follows:

# Parse "sys.argv" (default behaviour).
args = yadopt.parse(__doc__)

# Parse the given argument vector "argv".
args = yadopt.parse(__doc__, argv)

Use wrap function

YadOpt supports the decorator approach for command-line parsing by the decorator @yadopt.wrap which takes the same arguments as the function yadopt.parse.

@yadopt.wrap(__doc__)
def main(args: yadopt.YadOptArgs, arg1: int, arg2: str):
    ...

if __name__ == "__main__":
    main(arg1=1, arg2="2")

How to type arguments and options

YadOpt provides two ways to type arguments and options: (1) type name postfix and (2) description head declaration.

(1) Type name postfix: Users can type arguments and options by adding a type name at the end of the arguments/options name, such as the following:

Options:
    --opt1 FLT    Option of float type.
    --opt2 STR    Option of string type.

(2) Description head declaration: An alternative way to type arguments and options is to precede the description with the type name in parentheses.

Options:
    --opt1 VAL1    (float) Option of float type.
    --opt2 VAL2    (str)   Option of string type.

The following is the list of available type names.

Data type in Python Type name in YadOpt
bool bool, BOOL, boolean, BOOLEAN
int int, INT, integer, INTEGER
float flt, FLT, float FLOAT
str str, STR, string, STRING
pathlib.Path path, PATH

Dictionary and named tuple support

The returned value of yadopt.parse is an instance of YadOptArgs, a regular mutable Python class. However, sometimes a dictionary with the get accessor, or an immutable named tuple, may be preferable. In such cases, please try yadopt.to_dict or yadopt.to_namedtuple function.

# Convert the returned value to dictionary.
args = yadopt.to_dict(yadopt.parse(__doc__))

# Convert the returned value to namedtuple.
args = yadopt.to_namedtuple(yadopt.parse(__doc__))

Restore arguments from JSON file

YadOpt has a function to save parsed argument instances as a JSON file and to restore the argument instances from the JSON files. These functions are probably useful when recalling the same arguments that were previously executed, for example, in machine learning code.

# At first, create a parsed arguments (i.e. YadOptArgs instance).
args = yadopt.parse(__doc__)

# Save the parsed arguments as a JSON file.
yadopt.save("args.json", args)

# Resotre parsed YadOptArgs instance from the JSON file.
args_restored = yadopt.load("args.json")

The format of the JSON file is pretty straightforward — what the user types on the command line is stored in the "argv" key, and the docstring is stored in the "docstr" key in the JSON file. If users want to write the JSON file manually, the author recommends making a JSON file using the yadopt.save function and investigating the contents of the file.

API reference

See API reference page of the online document.

Developer's note

Preparation

Additional commands and Python packages are required for developers to measure the number of lines in the code, code quality, etc. Please run the following command (the author recommends using venv to avoid polluting your development environment).

$ apt install cloc
$ pip install -r requirements-dev.txt

Utility commands for developers

Utility commands are summarized in the Makefile. Please run make at the root directory of this repository to see the details of the subcommands in the Makefile.

$ make
Usage:
    make <command>

Build commands:
    build         Build package
    testpypi      Upload package to TestPyPi
    pypi          Upload package to PyPi
    install-test  Install from TestPyPi

Test and code check commands:
    check         Check the code quality
    count         Count the lines of code
    coverage      Measure code coverage

Other commands:
    clean         Cleanup cache files
    help          Show this message

Architecture diagram

software_architecture

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

yadopt-2025.6.18.tar.gz (102.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

yadopt-2025.6.18-py3-none-any.whl (23.8 kB view details)

Uploaded Python 3

File details

Details for the file yadopt-2025.6.18.tar.gz.

File metadata

  • Download URL: yadopt-2025.6.18.tar.gz
  • Upload date:
  • Size: 102.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for yadopt-2025.6.18.tar.gz
Algorithm Hash digest
SHA256 021395072231f777ec33cdd9ec5008fea991d0f2043aeb1893b2eea98df330c6
MD5 f8d2e33bb3dc59ed33f01e50735dee5f
BLAKE2b-256 0d9ceb63e0e45c19aa7b7361be45cb993bbed9f0aa340064496143771ca3c1d5

See more details on using hashes here.

File details

Details for the file yadopt-2025.6.18-py3-none-any.whl.

File metadata

  • Download URL: yadopt-2025.6.18-py3-none-any.whl
  • Upload date:
  • Size: 23.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for yadopt-2025.6.18-py3-none-any.whl
Algorithm Hash digest
SHA256 5e511117a6eaa1c22697c5a643363bb531ed866831f7e63af8a2ccd3cf8bece2
MD5 c0323d0cee06bf1dc9886d1b20fd66ce
BLAKE2b-256 394131fd67d48e08c1b212ffa8ad37eea93c62efa8b02e47b625c6b4d568cae3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page