dsargparse: docstring based argparse

dsargparse

dsargparse is a wrapper of argparse library which prepares helps and descriptions from docstrings. It also sets up functions to be run for each sub command, and provides a helper function which parses args and run a selected command.

Using this library, you don’t need to write same texts in docstrings, help, and description.

Install

Use pip to install from PyPi.

$ pip install -U dsargparse

Usage

dsargparse is a simple wrapper of the original argparse. To use it, install this package and just adding ds to your import command i.e. from import argparse to import dsargparse.

In addition to all API argparse has, dsargparse updates three functions;

and give one new method ArgumentParser.parse_and_run.

dsargparse.ArgumentParser

In addition to the keyword arguments argparse.ArgumentParser takes, this constructor has keyword argument main which takes the main function.

If you give the main function, you don’t need to set description, and formatter_class also will be set automatically.

add_argument

This method adds a new argument to the current parser. The function is same as argparse.ArgumentParser.add_argument. But, this method tries to determine help messages for the adding argument from some docstrings.

If the new arguments belong to some subcommand, the docstring of a function implements behavior of the subcommand has Args: section, and defines same name variable, this function sets such definition to the help message.

add_parser

After constructing subparsers by subparsers = parser.add_subparsers(), you may call subparsers.add_parser to add a new subcommand.

The add_parser has a new positional argument func which takes a function to be called in order to run the subcommand. The func will be used to determine the name, help, and description of this subcommand. The function func will also be set as a default value of cmd attribute.

The add_parser also has as same keyword arguments as add_parser of argparse library.

ArgumentParser.parse_and_run

This method parses arguments and run the selected command. It returns a value which the selected command returns. This function takes as same arguments as ArgumentParser.parse_args.

API References

Indices and tables

License

This software is released under the MIT License.