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
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 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 |
|
subcommand_name: Optional[str]
property
Gets the name of the chosen subcommand associated with the type of the object's subcommand field.
Returns:
Type | Description |
---|---|
Optional[str]
|
Name of the subcommand, if a subcommand field exists, and |
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 |
ArgParser
|
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
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 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 |
|
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 |
Union[ArgumentParser, _ArgumentGroup]
|
|
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_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
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 lists)const
: constant value required by some action/nargs combinationschoices
: list of possible inputs allowedhelp
: help stringmetavar
: name for the argument in usage messagesrequired
: whether the option is requiredgroup
: name of the argument group in which to put the argument; the group will be created if it does not already exist in the parserexclusive_group
: name of the mutually exclusive argument group in which to put the argument; the group will be created if it does not already exist in the parsersubcommand
: boolean flag marking this field as a subcommandparse_exclude
: boolean flag indicating that the field should not be included in the parserdefault_help
: boolean flag indicating the field's default value (if present) should be shown in the help- If
None
, falls back on the class-leveldefault_help
flag
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:- A boolean flag if its type is
bool
- Can set
action
in metadata as either"store_true"
(default) or"store_false"
- Can set
- An option if it specifies a default value
- Otherwise, a positional argument
- A boolean flag if its type is
- If
required
is specified in the metadata, this will take precedence over the default behavior above.
Source code in fancy_dataclass/cli.py
ArgparseDataclassSettings
Bases: MixinSettings
Class-level settings for the ArgparseDataclass
mixin.
Subclasses of ArgparseDataclass
may set the following fields as keyword arguments during inheritance:
parser_class
: subclass ofargparse.ArgumentParser
to use for argument parsingformatter_class
: subclass ofargparse.HelpFormatter
to use for customizing the help outputhelp_descr
: string to use for the help description, which is displayed when--help
is passed to the parser- If
None
, the class's docstring will be used by default.
- If
help_descr_brief
: string to use for the brief help description, which is used when the class is used as a subcommand entry. This is the text that appears in the menu of subcommands, which is often briefer than the main description.- If
None
, the class's docstring will be used by default (lowercased).
- If
command_name
: when this class is used to define a subcommand, the name of that subcommandversion
: if set to a string, expose a--version
argument displaying the version automatically (seeargparse
docs)default_help
: if set toTrue
, includes each field's default value in its help string (this can be overridden by the field-leveldefault_help
flag)
Source code in fancy_dataclass/cli.py
CLIDataclass
Bases: 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
|
Source code in fancy_dataclass/cli.py
run()
Runs the main body of the program.
Subclasses should implement this to provide custom behavior.
If the class has a subcommand defined, and it is an instance of CLIDataclass
, the default implementation of run
will be to call the subcommand's own implementation.