File: command-line.rst

package info (click to toggle)
python-oslo.config 1%3A8.3.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,192 kB
  • sloc: python: 10,740; makefile: 30; sh: 10
file content (41 lines) | stat: -rw-r--r-- 1,119 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
======================
 Command Line Options
======================

Positional Command Line Arguments
---------------------------------

Positional command line arguments are supported via a 'positional' Opt
constructor argument:

.. code-block:: console

    >>> conf = cfg.ConfigOpts()
    >>> conf.register_cli_opt(cfg.MultiStrOpt('bar', positional=True))
    True
    >>> conf(['a', 'b'])
    >>> conf.bar
    ['a', 'b']

By default, positional arguments are also required. You may opt-out of this
behavior by setting ``required=False``, to have an optional positional
argument.

Sub-Parsers
-----------

It is also possible to use argparse "sub-parsers" to parse additional
command line arguments using the SubCommandOpt class:

.. code-block:: console

    >>> def add_parsers(subparsers):
    ...     list_action = subparsers.add_parser('list')
    ...     list_action.add_argument('id')
    ...
    >>> conf = cfg.ConfigOpts()
    >>> conf.register_cli_opt(cfg.SubCommandOpt('action', handler=add_parsers))
    True
    >>> conf(args=['list', '10'])
    >>> conf.action.name, conf.action.id
    ('list', '10')