Getting stated with strings is a simple as calling clime() on an attrs class:
import attr
from clime import clime
@attr.s(auto_attribs=True)
class Dude:
name: str # attributes without default values in the class will be set as positional arguments in the cli
def state_name(self):
print(f"hi! my name is {self.name}")
def main():
clime(Dude).state_name()
if __name__ == "__main__":
main()
$ python main.py
usage: tutorial_001.py [-h] name
positional arguments:
name
optional arguments:
-h, --help show this help message and exit
Cool!
- Dude is a normal attrs decorated class.
- Name is a an argument type annotated with
str. - CliMe takes the class and turns it into a CLI with the builtin ArgumentParser. Try it!
$ python main.py joe
hi! my name is joe
Want to know what will not work? No arguments:
$ python main.py
usage: tutorial_001.py [-h] name
tutorial_001.py: error: the following arguments are required: name
Or using --name as on optional argument..
$ python main.py
usage: tutorial_001.py [-h] name
tutorial_001.py: error: unrecognized arguments: --name
Anyways... that's it.
All you need to take positional strings is an attrs decorated class with a str annotated class variable.