|
246 | 246 | "class Param:\n",
|
247 | 247 | " \"A parameter in a function used in `anno_parser` or `call_parse`\"\n",
|
248 | 248 | " def __init__(self, help=\"\", type=None, opt=True, action=None, nargs=None, const=None,\n",
|
249 |
| - " choices=None, required=None, default=None):\n", |
250 |
| - " if type in (store_true,bool): type,action,default=None,'store_true' ,False\n", |
| 249 | + " choices=None, required=None, default=None, version=None):\n", |
| 250 | + " if type in (store_true,bool): type,action,default=None,'store_true',False\n", |
251 | 251 | " if type==store_false: type,action,default=None,'store_false',True\n",
|
252 | 252 | " if type and isinstance(type,typing.Type) and issubclass(type,enum.Enum) and not choices: choices=list(type)\n",
|
253 | 253 | " help = help or \"\"\n",
|
254 | 254 | " store_attr()\n",
|
255 | 255 | "\n",
|
256 | 256 | " def set_default(self, d):\n",
|
| 257 | + " if self.action == \"version\":\n", |
| 258 | + " if d != inspect.Parameter.empty: self.version = d\n", |
| 259 | + " self.opt = True\n", |
| 260 | + " return\n", |
257 | 261 | " if self.default is None:\n",
|
258 |
| - " if d==inspect.Parameter.empty: self.opt = False\n", |
| 262 | + " if d == inspect.Parameter.empty: self.opt = False\n", |
259 | 263 | " else: self.default = d\n",
|
260 | 264 | " if self.default is not None:\n",
|
261 | 265 | " self.help += f\" (default: {self.default})\"\n",
|
|
288 | 292 | "cell_type": "markdown",
|
289 | 293 | "metadata": {},
|
290 | 294 | "source": [
|
291 |
| - "Each parameter in your function should have an annotation `Param(...)`. You can pass the following when calling `Param`: `help`,`type`,`opt`,`action`,`nargs`,`const`,`choices`,`required` (i.e. it takes the same parameters as `argparse.ArgumentParser.add_argument`, plus `opt`). Except for `opt`, all of these are just passed directly to `argparse`, so you have all the power of that module at your disposal. Generally you'll want to pass at least `help` (since this is provided as the help string for that parameter) and `type` (to ensure that you get the type of data you expect).\n", |
| 295 | + "Each parameter in your function should have an annotation `Param(...)`. You can pass the following when calling `Param`: `help`,`type`,`opt`,`action`,`nargs`,`const`,`choices`,`required`, `version` (i.e. it takes the same parameters as `argparse.ArgumentParser.add_argument`, plus `opt`). Except for `opt`, all of these are just passed directly to `argparse`, so you have all the power of that module at your disposal. Generally you'll want to pass at least `help` (since this is provided as the help string for that parameter) and `type` (to ensure that you get the type of data you expect).\n", |
292 | 296 | "\n",
|
293 | 297 | "`opt` is a bool that defines whether a param is optional or required (positional) - but you'll generally not need to set this manually, because fastcore.script will set it for you automatically based on *default* values. You should provide a default (after the `=`) for any *optional* parameters. If you don't provide a default for a parameter, then it will be a *positional* parameter.\n",
|
294 | 298 | "\n",
|
|
398 | 402 | "name": "stdout",
|
399 | 403 | "output_type": "stream",
|
400 | 404 | "text": [
|
401 |
| - "usage: progname [-h] [--b B] [--c {aa,bb,cc}] required a\n", |
| 405 | + "usage: progname [-h] [--v] [--b B] [--c {aa,bb,cc}] required a\n", |
402 | 406 | "\n",
|
403 | 407 | "my docs\n",
|
404 | 408 | "\n",
|
405 | 409 | "positional arguments:\n",
|
406 | 410 | " required Required param\n",
|
407 | 411 | " a param 1\n",
|
408 | 412 | "\n",
|
409 |
| - "optional arguments:\n", |
| 413 | + "options:\n", |
410 | 414 | " -h, --help show this help message and exit\n",
|
| 415 | + " --v Print version\n", |
411 | 416 | " --b B param 2 (default: test)\n",
|
412 | 417 | " --c {aa,bb,cc} param 3 (default: aa)\n"
|
413 | 418 | ]
|
|
417 | 422 | "_en = str_enum('_en', 'aa','bb','cc')\n",
|
418 | 423 | "def f(required:Param(\"Required param\", int),\n",
|
419 | 424 | " a:Param(\"param 1\", bool_arg),\n",
|
| 425 | + " v:Param(\"Print version\", action='version', version='%(prog)s 2.0.0'),\n", |
420 | 426 | " b:Param(\"param 2\", str)=\"test\",\n",
|
421 | 427 | " c:Param(\"param 3\", _en)=_en.aa):\n",
|
422 | 428 | " \"my docs\"\n",
|
|
426 | 432 | "p.print_help()"
|
427 | 433 | ]
|
428 | 434 | },
|
| 435 | + { |
| 436 | + "cell_type": "markdown", |
| 437 | + "metadata": {}, |
| 438 | + "source": [ |
| 439 | + "We can also check the version and help flaggs are working (you can ignore the error message below, this is expected behavior)" |
| 440 | + ] |
| 441 | + }, |
| 442 | + { |
| 443 | + "cell_type": "code", |
| 444 | + "execution_count": null, |
| 445 | + "metadata": {}, |
| 446 | + "outputs": [ |
| 447 | + { |
| 448 | + "name": "stdout", |
| 449 | + "output_type": "stream", |
| 450 | + "text": [ |
| 451 | + "progname 2.0.0\n" |
| 452 | + ] |
| 453 | + }, |
| 454 | + { |
| 455 | + "ename": "SystemExit", |
| 456 | + "evalue": "0", |
| 457 | + "output_type": "error", |
| 458 | + "traceback": [ |
| 459 | + "An exception has occurred, use %tb to see the full traceback.\n", |
| 460 | + "\u001b[0;31mSystemExit\u001b[0m\u001b[0;31m:\u001b[0m 0\n" |
| 461 | + ] |
| 462 | + } |
| 463 | + ], |
| 464 | + "source": [ |
| 465 | + "p.parse_args(['--v'])" |
| 466 | + ] |
| 467 | + }, |
| 468 | + { |
| 469 | + "cell_type": "code", |
| 470 | + "execution_count": null, |
| 471 | + "metadata": {}, |
| 472 | + "outputs": [ |
| 473 | + { |
| 474 | + "name": "stdout", |
| 475 | + "output_type": "stream", |
| 476 | + "text": [ |
| 477 | + "usage: progname [-h] [--v] [--b B] [--c {aa,bb,cc}] required a\n", |
| 478 | + "\n", |
| 479 | + "my docs\n", |
| 480 | + "\n", |
| 481 | + "positional arguments:\n", |
| 482 | + " required Required param\n", |
| 483 | + " a param 1\n", |
| 484 | + "\n", |
| 485 | + "options:\n", |
| 486 | + " -h, --help show this help message and exit\n", |
| 487 | + " --v Print version\n", |
| 488 | + " --b B param 2 (default: test)\n", |
| 489 | + " --c {aa,bb,cc} param 3 (default: aa)\n" |
| 490 | + ] |
| 491 | + }, |
| 492 | + { |
| 493 | + "ename": "SystemExit", |
| 494 | + "evalue": "0", |
| 495 | + "output_type": "error", |
| 496 | + "traceback": [ |
| 497 | + "An exception has occurred, use %tb to see the full traceback.\n", |
| 498 | + "\u001b[0;31mSystemExit\u001b[0m\u001b[0;31m:\u001b[0m 0\n" |
| 499 | + ] |
| 500 | + } |
| 501 | + ], |
| 502 | + "source": [ |
| 503 | + "p.parse_args(['-h'])" |
| 504 | + ] |
| 505 | + }, |
429 | 506 | {
|
430 | 507 | "cell_type": "markdown",
|
431 | 508 | "metadata": {},
|
|
450 | 527 | " required Required param\n",
|
451 | 528 | " a param 1\n",
|
452 | 529 | "\n",
|
453 |
| - "optional arguments:\n", |
| 530 | + "options:\n", |
454 | 531 | " -h, --help show this help message and exit\n",
|
455 | 532 | " --b B param 2 (default: test)\n",
|
456 | 533 | " --c {aa,bb,cc} param 3 (default: aa)\n"
|
|
0 commit comments