Step 3: Two positional arguments
Contents
Step 3: Two positional arguments¶
For the parameters that are not optional, it makes sense to parse them in a positional manner, i.e. take them from the command line arguments without them being preceded by a flag.
Now, for both x
and y
, we set their long_flag_name
and
short_flag_name
fields to None, and set their
positional
field to ONCE
,
which means that a single value is expected for both.
Note how the usage description changed in the execution sample.
Code¶
"""
Calculator tutorial, step 3
"""
from dataclasses import dataclass
from typing_extensions import Annotated
from configpile import Config, Param, Positional, parsers
@dataclass(frozen=True)
class Calc(Config):
"""
Command-line tool that sums two floating point numbers
"""
#: First argument
x: Annotated[
float,
Param.store(
parsers.float_parser,
positional=Positional.ONCE,
short_flag_name=None,
long_flag_name=None,
),
]
#: Second argument
y: Annotated[
float,
Param.store(
parsers.float_parser,
positional=Positional.ONCE,
short_flag_name=None,
long_flag_name=None,
),
]
c = Calc.from_command_line_()
print(f"{c.x} + {c.y} = {c.x+c.y}")
Executions¶
$ python ../../examples/calculator3.py --help
usage: ../../examples/calculator3.py x y
Command-line tool that sums two floating point numbers
required arguments:
x First argument
y Second argument
$ python ../../examples/calculator3.py 1 3
1.0 + 3.0 = 4.0