Created
January 12, 2020 16:04
-
-
Save podhmo/79178908e25597c6d8adba6801d366ea to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}?") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}?") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def hello(*, name: str = "world"): | |
print(f"hello {name}") | |
def byebye(*, name: str): | |
print(f"byebye {name}") | |
# ignored | |
def _ignore(name: str): | |
print("ignored") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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