项目作者: relastle

项目描述 :
:snowflake: Building Python CLI using docstrings and typehints :snake:
高级语言: Python
项目地址: git://github.com/relastle/glacier.git
创建时间: 2020-08-23T15:57:42Z
项目社区:https://github.com/relastle/glacier

开源协议:MIT License

下载


image

glacier

glacier is a python CLI building library for minimalists.






Installation

  1. pip install glacier

Quick start

You only have to call glacier against the entrypoint function.

  1. from glacier import glacier
  2. def main(name: str, verbose: bool = False) -> None:
  3. pass
  4. if __name__ == '__main__':
  5. glacier(main)

Then, you can see help 🍰.

quick start help

Basic Usage

CLI without subcommand

If you just call glacier to a function, it will invoke it as stand-alone CLI
(like the example in Quick start).

CLI with subcommands

You can easily construct CLI with subcommands in the following two ways.

Pass a list of functions

  1. from glacier import glacier
  2. def run(name: str, verbose: bool = False) -> None:
  3. """ Run """
  4. pass
  5. def build(name: str, verbose: bool = False) -> None:
  6. """ Build """
  7. pass
  8. def test(name: str, verbose: bool = False) -> None:
  9. """ Test """
  10. return
  11. if __name__ == '__main__':
  12. glacier([run, build, test])

If you passes a lift of function, glacier constructs the CLI with subcommands whose names are the same as the declared function names.
In this example, the subcommans will be run, build, and test.

sub commands help

Pass a dictionary of functions

You can easily give the different name as the subcommand name from any declared name of the function.
Just give a dictionary (key will be a subcommand name).

  1. from glacier import glacier
  2. def f1(name: str, verbose: bool = False) -> None:
  3. pass
  4. def f2(name: str, verbose: bool = False) -> None:
  5. pass
  6. def f3(name: str, verbose: bool = False) -> None:
  7. pass
  8. if __name__ == '__main__':
  9. glacier({
  10. 'run': f1,
  11. 'build': f2,
  12. 'test': f3,
  13. })

This works exactly the same as the previous example.

This interface makes it very easy to build a simple CLI tool from an existing project.

Async entrypoint support

You sometimes want your async function to be a CLI entrypoint.
Only you have to do is just passing the async function as if it were sync function.

The example below combine two async functions and a sync function into CLI
with nested subcommand structure.

  1. from glacier import glacier
  2. async def main() -> None:
  3. return
  4. def sub_1() -> None:
  5. return
  6. async def sub_2() -> None:
  7. return
  8. if __name__ == '__main__':
  9. glacier({
  10. 'main': main,
  11. 'sub': [
  12. sub_1,
  13. sub_2,
  14. ],
  15. })

Positional argument

If the name of function argument is underscore-prefiexed, it is understood as positional argument.

  1. from glacier import glacier
  2. def all_positional(_a: str, _b: str, _c: str) -> None:
  3. print(_a)
  4. print(_b)
  5. print(_c)
  6. if __name__ == '__main__':
  7. glacier(all_positional)

The above example is invoked as follows

  1. <command_name> <value of a> <value of b> <value of c>

Options

All other (non-underscore-prefixed) arguments are understood as options.

  1. from glacier import glacier
  2. def all_options(a: str, b: str, c: str) -> None:
  3. print(a)
  4. print(b)
  5. print(c)
  6. if __name__ == '__main__':
  7. glacier(all_options)

The above example is invoked as follows

  1. <command_name> --a <value of a> --b <value of b> --c <value of c>

Default value for optional argument

If you set the default value for function argument, it also defines the default value for CLI option.

  1. from glacier import glacier
  2. def default(verbose: bool = False) -> None:
  3. print(verbose)
  4. if __name__ == '__main__':
  5. glacier(default)

The above example is invoked as follows

  1. <command_name> # Just call without flag (`False` will be printed)

or

  1. <command_name> --verbose # Call with flag (`True` will be printed)

Help with docstring

Help message for options or command itself can be provided with python docstring.

Following style of doctrings are supported

The functions with docstring below will produce the exact the same help message with fed into glacier.
(You don’t need to specify which docstring style is used 😄)

Google Style

  1. def main_google(
  2. _path: str,
  3. name: str,
  4. verbose: bool = False,
  5. ) -> None:
  6. """
  7. This is my simple entry point of CLI.
  8. Args:
  9. _path: Positional argument representing the target file path.
  10. name: Name of this operation.
  11. verbose: Verbose output will be shown if set.
  12. """
  13. print(_path)
  14. print(name)
  15. print(verbose)
  16. return

Numpy Style

  1. def main_numpy(
  2. _path: str,
  3. name: str,
  4. verbose: bool = False,
  5. ) -> None:
  6. """
  7. This is my simple entry point of CLI.
  8. Parameters
  9. ----------
  10. _path: str
  11. Positional argument representing the target file path.
  12. name: str
  13. Name of this operation.
  14. verbose: bool
  15. Verbose output will be shown if set.
  16. """
  17. print(_path)
  18. print(name)
  19. print(verbose)
  20. return

reStructuredText Style

  1. def main_restructured_text(
  2. _path: str,
  3. name: str,
  4. verbose: bool = False,
  5. ) -> None:
  6. """
  7. This is my simple entry point of CLI.
  8. :param _path: Positional argument representing the target file path.
  9. :param name: Name of this operation.
  10. :param verbose: Verbose output will be shown if set.
  11. """
  12. print(_path)
  13. print(name)
  14. print(verbose)
  15. return

Supported types

  • int
  • str
  • bool
  • Enum
  • List[int]
  • List[str]

Note

:apple: Philosophy

  • This library is made for building CLI quickly especially for personal use, so the features provided by it is not rich.

  • If you want to build really user-friend CLI or that in production, I suggest that you use click (actually glacier uses it internally), or other full-stack CLI builing libraries.

:construction: Warnings

  • Please note that any destructive change (backward incompatible) can be done without any announcement.
  • This plugin is in a very early stage of development. Feel free to report problems or submit feature requests in Issues.

LICENSE

MIT