Type conversion works with Optional annotations, too

from typing import Optional

import attr

from clime import clime


@attr.s(auto_attribs=True)
class Dude:
    age_in_years: Optional[int] = None

    def volunteer_age(self):
        if self.age_in_years is not None:  # 0 counts!
            print(f"I am {self.age_in_years} years old.")
        else:
            print("A script never reveals its age")


def main():
    clime(Dude).volunteer_age()


if __name__ == "__main__":
    main()
Check it:

$ python docs_src/integers_floats_and_other_simple_classes/tutorial_002.py

usage: tutorial_002.py [-h] [--age-in-years AGE_IN_YEARS]

optional arguments:
  -h, --help            show this help message and exit
  --age-in-years AGE_IN_YEARS
                        type: <int> (default: None)


optional arguments:
  -h, --help    show this help message and exit

Cool!

  • age_in_years is annotated with Optional[int]
  • CliMe takes converts it an optional argument

Try it!

No arguments:

$ python docs_src/integers_floats_and_other_simple_classes/tutorial_002.py --age-in-years 12
I am 12 years old.

With arguments:

$ python docs_src/integers_floats_and_other_simple_classes/tutorial_002.py 
A script never reveals its age

That's it.