cli
ArgparseDataclass
Bases: DataclassMixin
Mixin class providing a means of setting up an argparse
parser with the dataclass fields, and then converting the namespace of parsed arguments into an instance of the class.
The parser's argument names and types will be derived from the dataclass's fields.
Per-field settings can be passed into the metadata
argument of each dataclasses.field
. See ArgparseDataclassFieldSettings
for the full list of settings.
Source code in fancy_dataclass/cli.py
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 |
|
args_to_dict(args)
classmethod
Converts a Namespace
object to a dict that can be converted to the dataclass type.
Override this to enable custom behavior.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
Namespace
|
|
required |
Returns:
Type | Description |
---|---|
Dict[str, Any]
|
A dict mapping from field names to values |
Source code in fancy_dataclass/cli.py
configure_argument(parser, name)
classmethod
Given an argument parser and a field name, configures the parser with an argument of that name.
Attempts to provide reasonable default behavior based on the dataclass field name, type, default, and metadata.
Subclasses may override this method to implement custom behavior.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
parser |
ArgumentParser
|
parser object to update with a new argument |
required |
name |
str
|
Name of the argument to configure |
required |
Source code in fancy_dataclass/cli.py
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
|
configure_parser(parser)
classmethod
Configures an argument parser by adding the appropriate arguments.
By default, this will simply call configure_argument
for each dataclass field.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
parser |
ArgumentParser
|
|
required |
Source code in fancy_dataclass/cli.py
from_args(args)
classmethod
Constructs an ArgparseDataclass
from a Namespace
object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
Namespace
|
|
required |
Returns:
Type | Description |
---|---|
Self
|
An instance of this class derived from the parsed arguments |
Source code in fancy_dataclass/cli.py
from_cli_args(arg_list=None)
classmethod
Constructs and configures an argument parser, then parses the given command-line arguments and uses them to construct an instance of the class.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
arg_list |
Optional[List[str]]
|
List of arguments as strings (if |
None
|
Returns:
Type | Description |
---|---|
Self
|
An instance of this class derived from the parsed arguments |
Source code in fancy_dataclass/cli.py
make_parser()
classmethod
Constructs an argument parser and configures it with arguments corresponding to the dataclass's fields.
Returns:
Type | Description |
---|---|
ArgumentParser
|
The configured |
Source code in fancy_dataclass/cli.py
new_parser()
classmethod
Constructs a new top-level argument parser..
Returns:
Type | Description |
---|---|
ArgumentParser
|
New top-level parser derived from the class's fields |
parser_argument_kwarg_names()
classmethod
Gets keyword argument names that will be passed when adding arguments to the argument parser.
Returns:
Type | Description |
---|---|
List[str]
|
Keyword argument names passed when adding arguments to the parser |
Source code in fancy_dataclass/cli.py
parser_class()
classmethod
Gets the type of the top-level argument parser.
Returns:
Type | Description |
---|---|
Type[ArgumentParser]
|
Type (subclass of |
parser_description()
classmethod
Gets a description string for the top-level argument parser, which will be displayed by default when --help
is passed to the parser.
By default, uses the class's own docstring.
Returns:
Type | Description |
---|---|
Optional[str]
|
String to be used as the program's description |
Source code in fancy_dataclass/cli.py
parser_kwargs()
classmethod
Gets keyword arguments that will be passed to the top-level argument parser.
Returns:
Type | Description |
---|---|
Dict[str, Any]
|
Keyword arguments passed upon construction of the |
Source code in fancy_dataclass/cli.py
process_args(parser, args)
classmethod
Processes arguments from an ArgumentParser, after they are parsed.
Override this to enable custom behavior.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
parser |
ArgumentParser
|
|
required |
args |
Namespace
|
|
required |
Source code in fancy_dataclass/cli.py
ArgparseDataclassFieldSettings
dataclass
Bases: FieldSettings
Settings for ArgparseDataclass
fields.
Each field may define a metadata
dict containing any of the following entries:
type
: override the dataclass field type with a different typeargs
: lists the command-line arguments explicitlyaction
: type of action taken when the argument is encounterednargs
: number of command-line arguments (use*
for lists,+
for non-empty listsconst
: constant value required by some action/nargs combinationschoices
: list of possible inputs allowedhelp
: help stringmetavar
: name for the argument in usage messagesgroup
: name of the argument group in which to put the argument; the group will be created if it does not already exist in the parserparse_exclude
: boolean flag indicating that the field should not be included in the parser
Note that these line up closely with the usual options that can be passed to ArgumentParser.add_argument
.
Positional arguments vs. options:
- If a field explicitly lists arguments in the
args
metadata field, the argument will be an option if the first listed argument starts with a dash, otherwise it will be a positional argument.- If it is an option but specifies no default value, it will be a required option.
- If
args
are absent, the field will be an option if either (1) it specifies a default value, or (2) it is a boolean type; otherwise it is a positional argument.
Source code in fancy_dataclass/cli.py
CLIDataclass
Bases: ABC
, ArgparseDataclass
This subclass of ArgparseDataclass
allows the user to execute arbitrary program logic using the parsed arguments as input.
Subclasses should override the run
method to implement custom behavior.
Source code in fancy_dataclass/cli.py
main(arg_list=None)
classmethod
Executes the following procedures in sequence:
- Constructs a new argument parser.
- Configures the parser with appropriate arguments.
- Parses command-line arguments.
- Post-processes the arguments.
- Constructs a dataclass instance from the parsed arguments.
- Runs the main body of the program, using the parsed arguments.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
arg_list |
Optional[List[str]]
|
List of arguments as strings (if |
None
|