Examples¶
Basic sqaure root¶
from ezcliy import Command, Positional
class Example(Command):
number = Positional('the number to be squared')
number.description = 'should be an int'
require_all_defined_positionals = True
def invoke(self):
print(int(self.number) ** 2)
if __name__ == '__main__':
Example().cli_entry()
Example calls¶
>>> python3 example.py 2
4
>>> python3 example.py 4
16
>>> python3 example.py --help
Usage: example [the number to be squared] [PARAMETERS]
Positionals:
the number to be squared -> should be an int
Flags:
--help -> Shows help.
Small cli calculator¶
import math
from ezcliy import Command, Positional, Flag
class QMath(Command):
name = 'qmath.py'
none_args_will_not_trigger_help = False
floor = Flag('-f', '--floor')
ceil = Flag('-c', '--ceil')
class Sum(Command):
description = 'Sums all passed values.'
floor: Flag # Reuse parent's parameters
ceil: Flag
def invoke(self):
result = sum(map(lambda v: float(v), self.values))
if self.floor.value:
print(math.floor(result))
elif self.ceil.value:
print(math.ceil(result))
else:
print(result)
class Product(Command):
description = 'Product of all passed values.'
def invoke(self):
print(math.prod(map(lambda v: float(v), self.values)))
class Root(Command):
x = Positional('x')
n = Positional('n')
require_all_defined_positionals = True
def invoke(self):
print(float(self.x.value) ** 1 / float(self.n.value))
if __name__ == '__main__':
QMath().cli_entry()
>>> python3 qmath.py
Usage: qmath.py [VALUES...] [COMMANDS] [PARAMETERS]
Commands:
sum -> Sums all passed values.
product -> Product of all passed values.
root
Flags:
--help -> Shows help even when no arguments are passed.
-f --floor
-c --ceil
>>> python3 qmath.py sum --help
Usage: sum [VALUES...] [PARAMETERS]
Description:
Sums all passed values.
Flags:
--help -> Shows help even when no arguments are passed.
-f --floor
-c --ceil
>>> python3 qmath.py sum 9 21 90 11.9
131.9
>>> python3 qmath.py sum 0.4 0.5 -c
1
>>> python3 qmath.py sum 0.4 0.5 --floor
0
>>> python3 qmath.py root 4 0.5
8.0