Skip to content

Instantly share code, notes, and snippets.

@podhmo podhmo/01hello.py
Created Jan 12, 2020

Embed
What would you like to do?
import typing as t
from handofcats import as_command
@as_command
def hello(name: str, nick_name: t.Optional[str] = None) -> None:
print(f"hello {name}")
if nick_name is not None:
print(f" {nick_name}?")
import typing as t
def hello(name: str, nick_name: t.Optional[str] = None) -> None:
print(f"hello {name}")
if nick_name is not None:
print(f" {nick_name}?")
import typing as t
from handofcats import as_subcommand
@as_subcommand
def hello(name: str, nick_name: t.Optional[str] = None) -> None:
print(f"hello {name}")
if nick_name is not None:
print(f" {nick_name}?")
@as_subcommand
def byebye(names: t.List[str]) -> None:
print(f"byebye {', '.join(names)}")
as_subcommand.run()
def hello(*, name: str = "world"):
print(f"hello {name}")
def byebye(*, name: str):
print(f"byebye {name}")
# ignored
def _ignore(name: str):
print("ignored")
import typing as t
def hello(name: str, nick_name: t.Optional[str] = None) -> None:
print(f"hello {name}")
if nick_name is not None:
print(f" {nick_name}?")
def byebye(names: t.List[str]) -> None:
print(f"byebye {', '.join(names)}")
def main(argv: t.Optional[t.List[str]] = None) -> t.Any:
import argparse
parser = argparse.ArgumentParser(formatter_class=type('_HelpFormatter', (argparse.ArgumentDefaultsHelpFormatter, argparse.RawTextHelpFormatter), {}))
subparsers = parser.add_subparsers(title='subcommands', dest='subcommand')
subparsers.required = True
fn = hello
sub_parser = subparsers.add_parser(fn.__name__, help=fn.__doc__, formatter_class=parser.formatter_class)
sub_parser.add_argument('name', help='-')
sub_parser.add_argument('--nick-name', required=False, help='-')
sub_parser.set_defaults(subcommand=fn)
fn = byebye # type: ignore
sub_parser = subparsers.add_parser(fn.__name__, help=fn.__doc__, formatter_class=parser.formatter_class)
sub_parser.add_argument('names', nargs='*', help='-')
sub_parser.set_defaults(subcommand=fn)
args = parser.parse_args(argv)
params = vars(args).copy()
subcommand = params.pop('subcommand')
return subcommand(**params)
if __name__ == '__main__':
main()
00:
python $(shell echo $@*.py)
01:
python $(shell echo $@*.py) -h
python $(shell echo $@*.py) foo
python $(shell echo $@*.py) foo --nick-name F
python $(shell echo $@*.py) --expose | tee exposed.py
02:
handofcats $(shell echo $@*.py) --expose | tee exposed.py
03:
python $(shell echo $@*.py) hello foo
python $(shell echo $@*.py) hello foo --nick-name F
python $(shell echo $@*.py) hello -h
python $(shell echo $@*.py) byebye foo bar boo
python $(shell echo $@*.py) byebye -h
python $(shell echo $@*.py) --expose | tee exposed.py
04:
handofcats $(shell echo $@*.py) -h
handofcats $(shell echo $@*.py) hello -h
handofcats $(shell echo $@*.py) byebye -h
handofcats $(shell echo $@*.py) --expose | tee exposed.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.