scriptconfig.value module¶
- class scriptconfig.value.Value(value=None, type=None, help=None, choices=None, position=None, isflag=False, nargs=None, alias=None, required=False, short_alias=None, group=None, mutex_group=None, tags=None)[source]¶
Bases:
NiceRepr
You may set any item in the config’s default to an instance of this class. Using this class allows you to declare the desired default value as well as the type that the value should be (Used when parsing sys.argv).
- Variables:
value (Any) – A float, int, etc…
type (type | None) – the “type” of the value. This is usually used if the value specified is not the type that self.value would usually be set to.
parsekw (dict) – kwargs for to argparse add_argument
position (None | int) – if an integer, then we allow this value to be a positional argument in the argparse CLI. Note, that values with the same position index will cause conflicts. Also note: positions indexes should start from 1.
isflag (bool) – if True, args will be parsed as booleans. Default to False.
alias (List[str] | None) – other long names (that will be prefixed with ‘–’) that will be accepted by the argparse CLI.
short_alias (List[str] | None) – other short names (that will be prefixed with ‘-’) that will be accepted by the argparse CLI.
group (str | None) – Impacts display of underlying argparse object by grouping values with the same type together. There is no other impact.
mutex_group (str | None) – Indicates that only one of the values in a group should be given on the command line. This has no impact on python usage.
tags (Any) – for external program use
CommandLine
xdoctest -m /home/joncrall/code/scriptconfig/scriptconfig/value.py Value xdoctest -m scriptconfig.value Value
Example
>>> self = Value(None, type=float) >>> print('self.value = {!r}'.format(self.value)) self.value = None >>> self.update('3.3') >>> print('self.value = {!r}'.format(self.value)) self.value = 3.3
- classmethod _from_action(action, actionid_to_groupkey, actionid_to_mgroupkey, pos_counter)[source]¶
Used in port_argparse
Example
import argparse from scriptconfig.value import * # NOQA action = argparse._StoreAction(‘foo’, ‘bar’, default=3) value = Value._from_action(action, {}, {}, 0)
action = argparse._CountAction(‘foo’, ‘bar’) value = Value._from_action(action, {}, {}, 0)
- class scriptconfig.value.Flag(value=False, **kwargs)[source]¶
Bases:
Value
Exactly the same as a Value except isflag default to True
- class scriptconfig.value.Path(value=None, help=None, alias=None)[source]¶
Bases:
Value
Note this is mean to be used only with scriptconfig.Config. It does NOT represent a pathlib object.
- class scriptconfig.value.PathList(value=None, type=None, help=None, choices=None, position=None, isflag=False, nargs=None, alias=None, required=False, short_alias=None, group=None, mutex_group=None, tags=None)[source]¶
Bases:
Value
Can be specified as a list or as a globstr
- FIXME:
will fail if there are any commas in the path name
Example
>>> from os.path import join >>> path = ub.modname_to_modpath('scriptconfig', hide_init=True) >>> globstr = join(path, '*.py') >>> # Passing in a globstr is accepted >>> assert len(PathList(globstr).value) > 0 >>> # Smartcast should separate these >>> assert len(PathList('/a,/b').value) == 2 >>> # Passing in a list is accepted >>> assert len(PathList(['/a', '/b']).value) == 2
- scriptconfig.value._value_add_argument_to_parser(value, _value, self, parser, key, fuzzy_hyphens=0)[source]¶
POC for a new simplified way for a value to add itself as an argument to a parser.
- Parameters:
value (Any) – the unwrapped default value
_value (Value) – the value metadata
- scriptconfig.value._value_add_argument_kw(value, _value, self, key, fuzzy_hyphens=0)[source]¶
TODO: resolve with
_value_add_argument_to_parser()
. This just creates one or more kwargs for add_argument. (Depending on how many variants of the argument we want).