Skip to main content

Library helps to build command line interface

Project description

A few libraries intend to help writing cli program more convenient.


Via pip:

$ pip install libcli



Hello world: examples/

from libcli import default, run

def main():
    print("Hello world!")

if __name__ == '__main__':

Define default and command

libcli.command or libcli.command(**kwargs) could be used as a decorator to define a command function.

Keyword arguments not starting with ‘_’ passed to libcli.command would be used as a hint to the option of the decorated function with the same name.

Keyword _name can be used to override the command name, otherwie the function name would be used.

libcli.default or libcli.default(**kwargs) is quite similar to command.

default function could only be defined once.

If defined, default function is always call at the very beginning.

Define error

libcli.command or libcli.command(**kwargs) could be used to decorate an exception.

libcli.command(**kwargs) could be used to define an errno other than default 127. would catch all defined errors, and exit with errno as exit code.

Define option type

by docstring: examples/

def main(*args, aflag=None, bflag=None, cvalue=None):
    """Parse the args.

    :param aflag: Set aflag
    :type aflag: flag.
    :param bflag: Set bflag
    :type bflag: flag.
    :param int cvalue: Set cvalue

by decorator hint: examples/

@default(aflag='_a', bflag='_b', cvalue='c:int,float')
def main(*args, aflag=None, bflag=None, cvalue):

otherwise guess from option default value.

Hint string: [_]shortopt[:[[[type1],type2],type3…] | ::[[[type1],type2],type3…]=default]

  • startswith a ‘_’ the option would be used as short option only, otherwise the option name would be used as a long option
  • shortopt should be a list of characters to be used as short option
  • without colons means this option requires no argument, if set function would be called with this option set a value not None.
  • a single colon means this option requires an argument
  • double colon measn the argument is optional, a default value is required
  • type list has priority

Possible types (not case sensitive):

  • str a command argument could always be parsed as a string
  • int, hex, dec, oct, bin parse argument as an integer, int accepts 0x, 0o, 0b, 0(c-style octal literal), default decimal
  • float parse as a floating point number
  • flag, none accept no argument, if set value will be not None, currently ‘’

following types may vary in future:

  • bool ‘0’, ‘n’, ‘no’, ‘f’, ‘false’, ‘nil’, ‘nul’, ‘null’, ‘none’, ‘-‘ is False, otherwise True
  • list a comma separated list, currently all values are supposed to be string
  • dict a comma separated key=value pair list, key and value are supposed to be string

Chained commands

If a command function returns a not None object, it would be used as the first argument of the next command function call.

Thus with a factory method as default, member functions returning self as command would do the trick.





Specify command with decorators. Automatically generate option list from decorator hinting, docstring and option default value. Parse a list of argumens and call corresponding method in a chainable style.


Yet another implementation to work close to GNU getopt. Unlike standard library, pylibcli.getopt employs an iterator interface. Optional argument supported. opttools use getopt internally.

Project details

Download files

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

Files for libcli, version 0.3.3
Filename, size File type Python version Upload date Hashes
Filename, size libcli-0.3.3-py3-none-any.whl (11.8 kB) File type Wheel Python version py3 Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page