Skip to content

Commit a6a2df9

Browse files
committed
fixes #245
1 parent fd100fc commit a6a2df9

File tree

2 files changed

+27
-22
lines changed

2 files changed

+27
-22
lines changed

fastcore/script.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def bool_arg(v):
2727
# Cell
2828
def clean_type_str(x:str):
2929
x = str(x)
30-
x = re.sub("(class|function|__main__\.|\ at.*)", '', x)
30+
x = re.sub("(enum |class|function|__main__\.|\ at.*)", '', x)
3131
x = re.sub("(<|>|'|\ )", '', x) # spl characters
3232
return x
3333

@@ -38,6 +38,7 @@ def __init__(self, help=None, type=None, opt=True, action=None, nargs=None, cons
3838
choices=None, required=None, default=None):
3939
if type==store_true: type,action,default=None,'store_true' ,False
4040
if type==store_false: type,action,default=None,'store_false',True
41+
if type and isinstance(type,typing.Type) and issubclass(type,enum.Enum) and not choices: choices=list(type)
4142
store_attr()
4243

4344
def set_default(self, d):
@@ -52,10 +53,10 @@ def pre(self): return '--' if self.opt else ''
5253
def kwargs(self): return {k:v for k,v in self.__dict__.items()
5354
if v is not None and k!='opt' and k[0]!='_'}
5455
def __repr__(self):
55-
if self.help is None and self.type is None: return ""
56-
if self.help is None and self.type is not None: return f"{clean_type_str(self.type)}"
57-
if self.help is not None and self.type is None: return f"<{self.help}>"
58-
if self.help is not None and self.type is not None: return f"{clean_type_str(self.type)} <{self.help}>"
56+
if not self.help and self.type is None: return ""
57+
if not self.help and self.type is not None: return f"{clean_type_str(self.type)}"
58+
if self.help and self.type is None: return f"<{self.help}>"
59+
if self.help and self.type is not None: return f"{clean_type_str(self.type)} <{self.help}>"
5960

6061
# Cell
6162
def anno_parser(func, prog=None, from_name=False):
@@ -65,8 +66,8 @@ def anno_parser(func, prog=None, from_name=False):
6566
param = func.__annotations__.get(k, Param())
6667
param.set_default(v.default)
6768
p.add_argument(f"{param.pre}{k}", **param.kwargs)
68-
p.add_argument(f"--pdb", help="Run in pdb debugger (default: False)", action='store_true')
69-
p.add_argument(f"--xtra", help="Parse for additional args (default: '')", type=str)
69+
p.add_argument(f"--pdb", help=argparse.SUPPRESS, action='store_true')
70+
p.add_argument(f"--xtra", help=argparse.SUPPRESS, type=str)
7071
return p
7172

7273
# Cell

nbs/08_script.ipynb

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@
198198
"#export\n",
199199
"def clean_type_str(x:str):\n",
200200
" x = str(x)\n",
201-
" x = re.sub(\"(class|function|__main__\\.|\\ at.*)\", '', x)\n",
201+
" x = re.sub(\"(enum |class|function|__main__\\.|\\ at.*)\", '', x)\n",
202202
" x = re.sub(\"(<|>|'|\\ )\", '', x) # spl characters\n",
203203
" return x"
204204
]
@@ -231,6 +231,7 @@
231231
" choices=None, required=None, default=None):\n",
232232
" if type==store_true: type,action,default=None,'store_true' ,False\n",
233233
" if type==store_false: type,action,default=None,'store_false',True\n",
234+
" if type and isinstance(type,typing.Type) and issubclass(type,enum.Enum) and not choices: choices=list(type)\n",
234235
" store_attr()\n",
235236
" \n",
236237
" def set_default(self, d):\n",
@@ -245,10 +246,10 @@
245246
" def kwargs(self): return {k:v for k,v in self.__dict__.items()\n",
246247
" if v is not None and k!='opt' and k[0]!='_'}\n",
247248
" def __repr__(self):\n",
248-
" if self.help is None and self.type is None: return \"\"\n",
249-
" if self.help is None and self.type is not None: return f\"{clean_type_str(self.type)}\"\n",
250-
" if self.help is not None and self.type is None: return f\"<{self.help}>\"\n",
251-
" if self.help is not None and self.type is not None: return f\"{clean_type_str(self.type)} <{self.help}>\""
249+
" if not self.help and self.type is None: return \"\"\n",
250+
" if not self.help and self.type is not None: return f\"{clean_type_str(self.type)}\"\n",
251+
" if self.help and self.type is None: return f\"<{self.help}>\"\n",
252+
" if self.help and self.type is not None: return f\"{clean_type_str(self.type)} <{self.help}>\""
252253
]
253254
},
254255
{
@@ -333,8 +334,8 @@
333334
" param = func.__annotations__.get(k, Param())\n",
334335
" param.set_default(v.default)\n",
335336
" p.add_argument(f\"{param.pre}{k}\", **param.kwargs)\n",
336-
" p.add_argument(f\"--pdb\", help=\"Run in pdb debugger (default: False)\", action='store_true')\n",
337-
" p.add_argument(f\"--xtra\", help=\"Parse for additional args (default: '')\", type=str)\n",
337+
" p.add_argument(f\"--pdb\", help=argparse.SUPPRESS, action='store_true')\n",
338+
" p.add_argument(f\"--xtra\", help=argparse.SUPPRESS, type=str)\n",
338339
" return p"
339340
]
340341
},
@@ -354,26 +355,27 @@
354355
"name": "stdout",
355356
"output_type": "stream",
356357
"text": [
357-
"usage: progname [-h] [--b B] [--pdb] [--xtra XTRA] required a\n",
358+
"usage: progname [-h] [--b B] [--c {aa,bb,cc}] required a\n",
358359
"\n",
359360
"my docs\n",
360361
"\n",
361362
"positional arguments:\n",
362-
" required Required param\n",
363-
" a param 1\n",
363+
" required Required param\n",
364+
" a param 1\n",
364365
"\n",
365366
"optional arguments:\n",
366-
" -h, --help show this help message and exit\n",
367-
" --b B param 2 (default: test)\n",
368-
" --pdb Run in pdb debugger (default: False)\n",
369-
" --xtra XTRA Parse for additional args (default: '')\n"
367+
" -h, --help show this help message and exit\n",
368+
" --b B param 2 (default: test)\n",
369+
" --c {aa,bb,cc} param 3 (default: aa)\n"
370370
]
371371
}
372372
],
373373
"source": [
374+
"_en = str_enum('_en', 'aa','bb','cc')\n",
374375
"def f(required:Param(\"Required param\", int),\n",
375376
" a:Param(\"param 1\", bool_arg),\n",
376-
" b:Param(\"param 2\", str)=\"test\"):\n",
377+
" b:Param(\"param 2\", str)=\"test\",\n",
378+
" c:Param(\"param 3\", en)=_en.aa):\n",
377379
" \"my docs\"\n",
378380
" ...\n",
379381
"\n",
@@ -512,6 +514,8 @@
512514
"Converted 01_basics.ipynb.\n",
513515
"Converted 02_foundation.ipynb.\n",
514516
"Converted 03_xtras.ipynb.\n",
517+
"Converted 03a_parallel.ipynb.\n",
518+
"Converted 03b_net.ipynb.\n",
515519
"Converted 04_dispatch.ipynb.\n",
516520
"Converted 05_transform.ipynb.\n",
517521
"Converted 07_meta.ipynb.\n",

0 commit comments

Comments
 (0)