Skip to main content

Absolutely trivial command line arguments

Project description

Doesn’t parsing command line arguments suck? At best, you use optparse, and then if that doesn’t work you hand-code it. Or, you use getargs, which requires writing your own loops. Don’t you wish for something a little bit… smarter? CLIArgs makes argument parsing a piece of cake.

Basics

Define a few functions, change your if __name__ == "__main__" block, and CLIArgs does the rest:

import cliargs

__help__ = "clone source [destinations] [-v]"
__version__ = "1.0"

def __main__(source, *dest, verbose=False):
    ...

if __name__ == "__main__":
    cliargs.main()

If you run this file (clone), the following command lines will all do the obvious:

clone a b c d e
clone --source=a b c d e
clone a b -v
clone --help
clone -?
clone --version

In general, all arguments to the function __main__ are examined, based on which the command line will be intelligently parsed. If necessary, the help and version info will be displayed. Finally, most errors in parsing command-lines are caught and a summary prepared for the user.

Argument types

The basic algorithm will examine the arguments of the function (that is, of __main__) and assign, for each arguments, a short and long argument, and a type. The long argument is always the name of argument; the short argument is, in order, the short name is the first letter of this short argument, or (if that is unavailable) the swapcase’d version of that letter, or one of a built-in list of exceptions. These are assigned left-to-right.

The type of any argument is by default assumed to be a string. However, if the argument is given a default value (as is verbose in the example above), the type of the default argument is used. If the type is a string, the command-line argument is simply passed to the function. However, integers and floats are converted into true integers and floats, booleans make their corresponding arguments into flags (such as verbose above), lists are created by splitting a single comma-delimited argument (that means that there cannot be spaces between the arguments; use --list=1,2,3,4 or similar constructions) and dictionaries by splitting by commas, then equal signs (e.g. --dict=a=1,b=2,c=3). Of course, list and dictionary arguments are usually better handled by the *args and **kwargs parameters.

Help & Version

The help information is taken from a function called __help__, a string named __help__, or the module documentation. If __help is a function, it is called, with all of the arguments after the first -? (or -h or –help`) passed as arguments; thus, it it best that __help__ take a variable number of arguments. Otherwise, __help__ is assumed to be a string and is printed. __version__ can likewise be either a string or a function; as a function, it is called without arguments.

Use Beyond Argument Parsing

Sometimes, we want to parse arguments other than those in sys.argv, such as for a built-in shell. One can use CLIArgs to provide a similarly-shiny tool for this purpose as well. Simply use the function cliargs.run(function, arguments, help=None, version=None).

Project details


Release history Release notifications | RSS feed

This version

1.0

Download files

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

Source Distribution

CLIArgs-1.0.tar.gz (17.6 kB view details)

Uploaded Source

File details

Details for the file CLIArgs-1.0.tar.gz.

File metadata

  • Download URL: CLIArgs-1.0.tar.gz
  • Upload date:
  • Size: 17.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for CLIArgs-1.0.tar.gz
Algorithm Hash digest
SHA256 63614e3503330b3d61bdf0a33a61fbe1d0d3ad445e420c1e93f7ab2e86392d1c
MD5 c0ba2f9c38b0f15502995936250f031d
BLAKE2b-256 c621fc4919b94def1dc7de73d52a14005310379c912a9401947ed6b619479614

See more details on using hashes here.

Supported by

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