Skip to content

Commit 4225fa3

Browse files
Add help= for aliases
Help=
2 parents 701ddd1 + 9d07a85 commit 4225fa3

File tree

6 files changed

+52
-14
lines changed

6 files changed

+52
-14
lines changed

README.rst

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ Define new commands in your config file which provide aliases to other commands.
376376
[[alias]]
377377
378378
name = "jsonl"
379-
short_help = "Load jsonlines into python objects."
379+
help = "Load jsonlines into python objects."
380380
381381
[[alias.stage]]
382382
@@ -419,7 +419,7 @@ Convenient for removing trailing commas.
419419
[[alias]]
420420
421421
name = "yml2json"
422-
short_help = "Convert yaml to json"
422+
help = "Convert yaml to json"
423423
424424
[[alias.stage]]
425425
@@ -455,7 +455,7 @@ Pull text out of xml documents.
455455
456456
[[alias]]
457457
name="xpath"
458-
short_help = "Find xml elements matching xpath query."
458+
help = "Find xml elements matching xpath query."
459459
arguments = [{name="query", type="str"}]
460460
inject_values=["query"]
461461
@@ -482,7 +482,7 @@ Generate json objects
482482
483483
484484
name="jo"
485-
short_help="Make json objects"
485+
help="Make json objects"
486486
arguments=[{name="pairs", type="str"}]
487487
inject_values=["pairs"]
488488
@@ -535,25 +535,57 @@ try:
535535
536536
.. code-block:: toml
537537
538+
base_exec_before = '''
539+
import csv
540+
import typing as t
541+
542+
543+
def read_csv(
544+
file, header: bool, **kwargs
545+
) -> t.Iterable[t.Dict[t.Union[str, int], str]]:
546+
"Read csv rows into an iterable of dicts."
547+
548+
rows = list(file)
549+
550+
first_row = next(csv.reader(rows))
551+
if header:
552+
fieldnames = first_row
553+
reader = csv.DictReader(rows, fieldnames=fieldnames, **kwargs)
554+
return list(reader)[1:]
555+
556+
fieldnames = range(len(first_row))
557+
return csv.DictReader(rows, fieldnames=fieldnames, **kwargs)
558+
559+
'''
560+
561+
562+
563+
538564
[[alias]]
539565
name = "csv"
540-
short_help = "Load csv rows into python objects"
541-
inject_values=["delimiter"]
566+
help = "Load csv rows into python dicts. With --no-header, keys will be numbered from 0."
567+
inject_values=["delimiter", "header"]
542568
543569
[[alias.options]]
544570
name = "--delimiter"
545571
default = ","
572+
help = "field delimiter character"
573+
574+
[[alias.options]]
575+
name = "--header/--no-header"
576+
default=true
577+
help = "Treat the first row as a header?"
546578
547579
[[alias.stage]]
548580
command = "apply"
549-
options = {code="csv.DictReader(x, delimiter=delimiter)"}
581+
options = {code="read_csv(x, header=header)"}
550582
551583
[[alias.stage]]
552584
command = "chain"
553585
554586
[[alias.stage]]
555587
command = "map"
556-
options = {code="dict"}
588+
options = {code="dict(x)"}
557589
558590
559591

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[bumpversion]
2-
current_version = 0.0.112
2+
current_version = 0.0.114
33
commit = True
44
tag = True
55

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
setuptools.setup(
1515
name="mario",
16-
version="0.0.112",
16+
version="0.0.114",
1717
description="Shell pipes for Python.",
1818
long_description=open(PROJECT_ROOT / "README.rst").read(),
1919
long_description_content_type="text/x-rst",

src/mario/_version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "0.0.112"
1+
__version__ = "0.0.114"

src/mario/aliasing.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ def make(self, validated, partial, many):
101101
@attr.dataclass
102102
class Alias:
103103
name: str
104-
short_help: str
104+
short_help: t.Optional[str]
105+
help: t.Optional[str]
105106
arguments: t.List[click.Argument]
106107
options: t.List[click.Option]
107108
stages: t.List[AliasStage]
@@ -110,7 +111,8 @@ class Alias:
110111

111112
class AliasSchema(marshmallow.Schema):
112113
name = fields.String()
113-
short_help = fields.String(default=None)
114+
help = fields.String(default=None, missing=None)
115+
short_help = fields.String(default=None, missing=None)
114116
arguments = fields.List(fields.Nested(ArgumentSchema), missing=list)
115117
options = fields.List(fields.Nested(OptionSchema), missing=list)
116118
stages = fields.List(fields.Nested(AliasStageSchema), data_key="stage")

src/mario/cli.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,11 @@ def run(ctx, **cli_params):
8080

8181
params = alias.arguments + alias.options
8282
return click.Command(
83-
name=alias.name, params=params, callback=click.pass_context(run)
83+
name=alias.name,
84+
params=params,
85+
callback=click.pass_context(run),
86+
short_help=alias.short_help,
87+
help=alias.help,
8488
)
8589

8690

0 commit comments

Comments
 (0)