.. |colon| replace:: colon |nbsp| (``:``) .. _reference: .. _parameter-reference: User reference ============== Clize deduces what kind of parameter to pick for the CLI depending on what kind of parameter is found on the Python function as well as its annotations. .. note:: For how parameters are converted between Python and CLI: * :ref:`pos param` * :ref:`extra posargs` * :ref:`named param` .. _using annotations: Using annotations ----------------- You can use annotations defined throughout this document to refine parameter conversion (or change it completely). Here's a primer on how to use these annotations. You can pass annotations as a sequence: .. code-block:: python def func(*, param:('p', int)): pass def func(*, param:['p', int]): pass When only one annotation is needed, you can omit the sequence: .. code-block:: python def func(*, param:'p'): pass def func(*, param:('p',)): pass def func(*, param:['p']): pass .. _param with value: Annotations for parameters that handle a value ---------------------------------------------- The positional and options parameters detailed later both handle the following features: .. index:: value conversion, value converter .. _value converter: Specifying a value converter ............................ A function or callable decorated with `.parser.value_converter` passed as annotation will be used during parsing to convert the value from the string found in `sys.argv` into a value suitable for the annotated function. .. code-block:: python from clize import run, parser @parser.value_converter def wrap_xy(arg): return 'x' + arg + 'y' def func(a, b:wrap_xy): print(repr(a), repr(b)) run(func) .. code-block:: console $ python valconv.py abc def 'abc' 'xdefy' ``def`` was transformed into ``xdefy`` because of the value converter. Besides callables decorated with `.parser.value_converter`, the built-in functions `int`, `float` and `bool` are also recognized as value converters. .. _included converters: Included value converters ......................... .. autofunction:: clize.converters.datetime .. autofunction:: clize.converters.file .. index:: default value .. _default value: Specifying a default value .......................... The parameter's default value is used without conversion. If no :ref:`value converter ` is specified, its type is used instead. When a default value is specified, the parameter becomes optional. :: >>> def func(par=3): ... print(repr(par)) ... >>> run(func, exit=False, args=['func', '46']) 46 >>> run(func, exit=False, args=['func']) 3 Therefore, be careful not to use values of types for which the constructor does not handle strings, unless you specify a converter:: >>> from datetime import datetime >>> now = datetime.utcnow() >>> def fail(par=now): ... print(repr(par)) ... >>> run(fail, exit=False, args=['func', '...']) Traceback (most recent call last): ... TypeError: an integer is required (got type str) >>> from dateutil.parser import parse >>> from datetime import datetime >>> now = datetime.utcnow() >>> def func(par:parse=now): ... print(par) ... >>> run(func, exit=False, args=['func', '1/1/2016']) 2016-01-01 00:00:00 .. index:: CLI default value .. index:: converted default value .. _converted default value: .. _cli default value: Specifying a default value that is converted and only used in the CLI ..................................................................... .. py:function:: clize.Parameter.cli_default(value, *, convert=True) Annotate a parameter with this to have Clize supply a default value, even if there wouldn't be a default value when calling the function normally from Python. Unless ``convert`` is ``False``, the value provided will be converted by clize. :: >>> from clize import run, Parameter >>> def func(par: Parameter.cli_default("4") = 3): ... return par ... >>> func() 3 >>> run(func, exit=False, args=['func']) 4 .. _force required: Ignoring the source parameter's default value ............................................. .. attribute:: clize.Parameter.REQUIRED Annotate a parameter with this to force it to be required, even if there is a default value in the source. :: >>> from clize import run, Parameter >>> def func(par:Parameter.REQUIRED=3): ... pass ... >>> run(func, exit=False, args=['func']) func: Missing required arguments: par Usage: func par .. _pos param: Positional parameters --------------------- Normal parameters in Python are turned into positional parameters on the CLI. Plain arguments on the command line (those that don't start with a ``-``) are processed by those and assigned in the order they appear: .. code-block:: python from clize import run def func(posparam1, posparam2, posparam3): print('posparam1', posparam1) print('posparam2', posparam2) print('posparam3', posparam3) run(func) .. code-block:: console $ python posparams.py one two three posparam1 one posparam2 two posparam3 three It also shares the features detailed in :ref:`param with value`. .. _extra posargs: Parameter that collects remaining positional arguments ------------------------------------------------------ An ``*args``-like parameter in Python becomes a repeatable positional parameter on the CLI: .. code-block:: python from clize import run def func(param, *args): print('param', param) print('args', args) run(func) .. code-block:: console $ python argslike.py one two three param one args ('two', 'three') .. attribute:: clize.Parameter.REQUIRED When used on an ``*args`` parameter, requires at least one value to be provided. You can use `clize.parameters.multi` for more options. .. _named param: Named parameters ---------------- Clize treats keyword-only parameters as named parameters, which, instead of their position, get designated by they name preceded by ``--``, or by ``-`` if the name is only one character long. There are a couple kinds of named parameters detailed along with examples in the sections below. They all understand annotations to specify alternate names for them: Simply pass them as strings in the parameter's annotation: .. code-block:: python from clize import run def func(*, par:('p', 'param')): print('par', par) run(func) .. code-block:: console $ python named.py --par value par value $ python named.py -p value par value $ python named.py --param value par value .. _name conversion: Name conversion ............... So you can best follow Python's naming conventions, Clize alters parameter names to CLI conventions so that they always match ``--kebap-case``. While Python recommends ``snake_case`` for identifiers such as parameter names, Clize also handles words denoted by capitalization: ===================== ======================= Python parameter name CLI parameter name ===================== ======================= ``param`` ``--param`` ``p`` ``-p`` ``snake_case`` ``--snake-case`` ``list_`` ``--list`` ``capitalizedOnce`` ``--capitalized-once`` ``CapitalizedTwice`` ``--capitalized-twice`` ===================== ======================= Clize replaces underscores (``_``) with dashes (``-``), and uppercase characters with a dash followed by the equivalent lowercase character. Dashes are deduplicated and removed from the extremities. Short option names (those that are only one letter) are prepended with one dash (``-``). Long option names are prepended with two dashes (``--``). No dash is prepended for subcommand names. .. note:: You do not need to consider this when :ref:`documenting parameters `. Simply match the way the parameter is written in your Python sources. .. _option param: Named parameters that take an argument -------------------------------------- Keyword-only parameters in Python become named parameters on the CLI: They get designated by their name rather than by their position: .. code-block:: python from clize import run def func(arg, *, o, par): print('arg', arg) print('o', o) print('par', par) run(func) .. code-block:: console $ python opt.py -o abc def --par ghi arg def o abc par ghi $ python opt.py -oabc --par=def ghi arg ghi o abc par def The parameter is designated by prefixing its name with two dashes (eg. ``--par``) or just one dash if the name is only one character long (eg. ``-o``). The value is given either as a second argument (first example) or glued to the parameter name for the short form, glued using an equals sign (``=``) for the long form. It shares the features of :ref:`param with value` and :ref:`named param`. .. _int option param: Named parameters that take an integer argument ---------------------------------------------- A variant of :ref:`option param` used when the value type from :ref:`param with value` is `int`. The only difference is that when designating the parameter using the short glued form, you can chain other short named parameters afterwards: .. code-block:: python from clize import run def func(*, i: int, o): print('i', i) print('o', o) run(func) .. code-block:: console $ python intopt.py -i42o abcdef i 42 o abcdef .. _flag parameter: Flag parameters --------------- Flag parameters are named parameters that unlike :ref:`options