Parser
Custom ArgumentParser extension for queued argument collection and task execution.
This module provides a subclass of argparse.ArgumentParser that supports custom queue-based actions. It allows arguments to be collected in an ordered dictionary (queue) and grouped into tasks for sequential processing.
It registers the following custom actions: - "q": general Queue action similar to 'store' - "q_true": stores True in the queue, similar to 'store_true' - "q_false": stores False in the queue, similar to 'store_false' - "task": marks a task boundary and collects queued arguments
It registers the following custom type:
- "kwargs": convert
ArgumentParser
Bases: _ActionsContainer, ArgumentParser
flowchart TD
src.qargparse.parser.ArgumentParser[ArgumentParser]
src.qargparse.parser._ActionsContainer[_ActionsContainer]
src.qargparse.parser._ActionsContainer --> src.qargparse.parser.ArgumentParser
click src.qargparse.parser.ArgumentParser href "" "src.qargparse.parser.ArgumentParser"
click src.qargparse.parser._ActionsContainer href "" "src.qargparse.parser._ActionsContainer"
Queue-aware ArgumentParser with task execution support.
This class extends argparse.ArgumentParser with queue-based
argument accumulation and task dispatch semantics.
In addition to standard argparse behavior, it:
- Registers custom queue actions (
q,q_true,q_false,task) - Tracks queued keyword arguments between task invocations
- Collects task execution blocks during parsing
- Removes internal bookkeeping attributes before returning
- Returns both the cleaned namespace and structured task data
The parser enables task-oriented CLI workflows where parameters are accumulated and executed in ordered blocks.
Source code in src/qargparse/parser.py
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 | |
parse_args(*args, namespace=None, **kwargs)
Parse CLI arguments and return structured task information.
This method extends the default argparse.ArgumentParser.parse_args
behavior by:
- Initializing internal queue and task storage attributes in the parsing namespace.
- Parsing arguments using the parent implementation.
- Extracting collected task blocks.
- Validating that no queued arguments remain unprocessed.
- Removing temporary queue-related attributes.
- Cleaning queue action destinations from the namespace.
- Merging default values across parser and groups.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*args
|
Positional arguments forwarded to
|
()
|
|
namespace
|
Namespace
|
Existing namespace to populate. If None, a new namespace is created. |
None
|
**kwargs
|
Additional keyword arguments forwarded to
|
{}
|
Returns:
| Type | Description |
|---|---|
Tuple[Namespace, TasksKwargs]
|
Tuple[argparse.Namespace, TasksKwargs]:
- namespace:
The parsed namespace with all temporary queue
attributes removed.
- tasks:
A |
Raises:
| Type | Description |
|---|---|
ValueError
|
If queued keyword arguments remain after parsing, indicating that a task trigger was missing. |
Source code in src/qargparse/parser.py
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 | |
__init__(*args, **kwargs)
Initialize the custom _ActionsContainer and register queue actions and cutsom type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*args
|
Positional arguments forwarded to argparse._ActionsContainer. |
()
|
|
**kwargs
|
Keyword arguments forwarded to argparse._ActionsContainer. |
{}
|
Source code in src/qargparse/parser.py
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | |
add_argument(*args, action='store', dest=None, default=..., alias='', **kwargs)
Add an argument with extended queue and alias support.
This method extends argparse._ActionsContainer.add_argument by:
- Supporting custom queue-based actions (
q,q_true,q_false,task). - Allowing alias-based parameter mapping for queue actions.
- Tracking per-destination default values for queued arguments.
When using the q action, an optional alias may be provided
to associate a task-specific default value with a parameter.
Aliases are internally prefixed to distinguish them at parse time.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*args
|
Positional arguments forwarded to |
()
|
|
action
|
str
|
The action type. Defaults to |
'store'
|
dest
|
str
|
Explicit destination name. If omitted for optional arguments, it is inferred automatically. |
None
|
default
|
Any
|
Default value for the argument. For queue actions, defaults may be stored internally per alias. |
...
|
alias
|
str
|
Optional alias name associated with the parameter.
Only valid when |
''
|
**kwargs
|
Additional keyword arguments forwarded to argparse. |
{}
|
Returns:
| Type | Description |
|---|---|
Action
|
argparse.Action: The created action object. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If alias is used with a non-queue action, if multiple string options are provided with alias, or if a duplicate alias is registered. |
Source code in src/qargparse/parser.py
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 | |
add_arguments(regular=[], q_global=[], tasks={}, task_prefix='--', alias=..., verbose=False, _parser_name='parser')
Add multiple arguments using structured task definitions.
This high-level helper allows defining:
- Regular argparse arguments
- Global queue arguments shared across tasks
- Task-specific argument groups
- Task launch triggers
Arguments are added in the following order:
regulararguments (standard argparse actions)q_globalarguments (shared queue actions)- Task-specific argument groups and their parameters
- Task trigger arguments (
ACTION_TASK)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
regular
|
List[Tuple[Union[str, List[str]], Dict[str, Any]]]
|
List of tuples |
[]
|
q_global
|
List[Union[Tuple[Union[str, List[str]], Dict[str, Any]], Tuple[Union[str, List[str]], Dict[str, Any], Dict[str, Any]]]]
|
List of tuples defining shared queue arguments. Each element may be:
where |
[]
|
tasks
|
Dict[str, Union[List[Tuple[Union[str, List[str]], Dict[str, Any]]], Tuple[List[Tuple[Union[str, List[str]], Dict[str, Any]]], Dict[str, Any]]]]
|
Mapping of task name to either:
|
{}
|
task_prefix
|
str
|
Prefix used for task trigger arguments (default: "--"). |
'--'
|
alias
|
str
|
Optional alias automatically applied to all queue arguments. |
...
|
verbose
|
bool
|
If True, prints generated parser-building code. |
False
|
_parser_name
|
str
|
Internal name used when printing verbose output. |
'parser'
|
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If invalid action types are used or if a task
referenced in |
Source code in src/qargparse/parser.py
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 | |
add_argument_group(*args, **kwargs)
Create and register a custom argument group.
This method overrides the base implementation to return a
specialized _ArgumentGroup instance that preserves
queue-aware behavior.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*args
|
Positional arguments forwarded to
|
()
|
|
**kwargs
|
Keyword arguments forwarded to the group constructor. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
_ArgumentGroup |
The created argument group instance. |
Source code in src/qargparse/parser.py
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 | |