Args
duho.args
AUTO = _AutoVersion()
module-attribute
Arg = _ty.Annotated
module-attribute
Factory = _ty.Callable[[str], _T]
module-attribute
NOT_DEFINED = _inspect.NOT_DEFINED
module-attribute
NS = _argparse.Namespace
module-attribute
__all__ = ['Append', 'Argument', 'ArgumentBuilder', 'ArgumentMeta', 'Args', 'Arg', 'Choice', 'Const', 'Count', 'Extend', 'Factory', 'main', 'NS', 'NOT_DEFINED', 'parse', 'print_completion', 'UpdateAction', 'value_sources']
module-attribute
Args
Bases: Namespace
Argument
Bases: Protocol
from_type(factory, **kwargs)
classmethod
Source code in src/duho/args.py
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 | |
ArgumentBuilder
Bases: Namespace
action = None
class-attribute
instance-attribute
choices = None
class-attribute
instance-attribute
const = NOT_DEFINED
class-attribute
instance-attribute
default
instance-attribute
env = None
class-attribute
instance-attribute
flags
instance-attribute
help
instance-attribute
metavar = None
class-attribute
instance-attribute
name
instance-attribute
nargs = None
class-attribute
instance-attribute
required = None
class-attribute
instance-attribute
type
instance-attribute
version = None
class-attribute
instance-attribute
add_to_parser(parser)
Source code in src/duho/args.py
557 558 559 560 561 562 563 564 565 | |
ArgumentMeta
Bases: _ProtocolMeta
__instancecheck__(instance)
Source code in src/duho/args.py
299 300 301 | |
UpdateAction
Bases: Action
Action that updates a dict instead of replacing it.
__call__(parser, namespace, values, option_string=None)
Source code in src/duho/args.py
779 780 781 782 783 784 785 | |
Append(type=str, **kw)
Create an append-action argument, accumulating repeated flag values.
Explicitly clears nargs: a bare list/list[T] annotation's implicit
builder defaults to action="extend", nargs="" (space-separated), which
would make append() collect a list* per occurrence instead of a scalar.
Source code in src/duho/args.py
757 758 759 760 761 762 763 764 | |
Choice(*choices, **kw)
Restrict an argument's accepted values to choices.
Source code in src/duho/args.py
772 773 774 | |
Const(value, **kw)
Create a store_const-action argument that stores value when present.
Source code in src/duho/args.py
767 768 769 | |
Count(**kw)
Create a count-action argument (e.g. -vvv -> 3).
Source code in src/duho/args.py
752 753 754 | |
Extend(split, **kwargs)
Create an extend-action argument with optional string splitting.
Source code in src/duho/args.py
736 737 738 739 740 741 742 743 744 745 746 747 748 749 | |
main(cls, argv=None, *, setup_logging=True, config=None)
Build a parser for cls, parse argv, and dispatch to instance.call().
Module-level (not a classmethod) so the Args subclass namespace stays
entirely user-owned. Steps: build parser (auto-registers subcommands),
apply the env/config/class-default layers (config overrides cls._config_;
precedence CLI > env > config > class default), parse argv (SystemExit from
argparse propagates), optionally set up stderr logging + apply verbosity
when the resulting instance provides set_loglevels, then call
instance() (i.e. instance.call()) and map a None return to 0.
Source code in src/duho/args.py
804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 | |
parse(spec, argv=None, *, parser_kwargs=None, config=None)
Build a parser from spec and parse argv into a new instance.
spec may be:
- An Args subclass (type): equivalent to spec._parser_().parse_args(argv),
with the env/config/class-default layers applied first (see below).
- An instance of an Args subclass: the instance's current field values
are used as argparse defaults (via parser.set_defaults(**overrides),
filtered to actual CLI fields -- not vars(spec), which would include
framework attrs). CLI args still override those defaults. Returns a
NEW instance of type(spec); spec itself is never mutated.
config (a path, or None to fall back to cls._config_) layers config-file
and environment-variable defaults under the instance/CLI ones. Full
precedence: CLI args > instance field values > env > config file > class
defaults. Note this means a required field (no class default) that is
supplied by any layer becomes effectively optional for this call.
Source code in src/duho/args.py
841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 | |
print_completion(cls, shell, file=None)
Print a shell completion script for cls to file (default sys.stdout).
Standalone counterpart to the --print-completion flag injected when
_completion_ = True -- builds cls's parser tree fresh (independent of
whether _completion_ is set) and delegates to duho.completion.<shell>.
Source code in src/duho/args.py
788 789 790 791 792 793 794 795 796 797 798 799 800 801 | |
value_sources(parsed)
Report the origin layer ("cli", "env", "config", or "default") of each
field on a parsed instance produced by duho.parse/duho.main.
Looks up the owning parser via the per-class _duho_last_parser_
linkage stashed during dispatch (see _initparser_). Returns {} if
unavailable (e.g. the instance wasn't produced via a parser built by
this framework, or no parse has happened yet for its class).
A field is "cli" if its parsed value differs from the effective default that was in effect for that parse -- the merged env/config value when the field was touched by one of those layers, else the class default. Otherwise it's whatever layer contributed that default ("env"/"config"), or "default" if no layer touched it (value == the untouched class default).
Source code in src/duho/args.py
893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 | |