|
Examples
Usage examples and documentation for pyopt.
Positional Command-Line FunctionsSee the project home for this example. Keyword Command-Line Functionsimport pyopt
expose = pyopt.Exposer()
@expose.kwargs
def bigfun(brightness:int, nudge:bool, happy:bool, shaft:str='gold'):
"""
bigfun is a keyword command-line function.
Switches can be given in any order.
Also, booleans can be combined ie: -nh
brightness - how bright is it outside on a scale of 1-10
nudge - are you nudging me?
happy - script should be happy.
shaft - what kind of shaft are you?
"""
if brightness < 10:
print("Always look on the bright side of life.")
print("You are a shaft of %s when all around is dark." % shaft)
if nudge:
print("Wink ;)")
if not happy:
print("Let's bicker and argue over who killed who.")
if __name__ == "__main__":
expose.run()Gives the following: Usage: example.py -b brightness [-s shaft] [-n] [-h]
bigfun is a keyword command-line function.
Switches can be given in any order.
Also, booleans can be combined ie: -nh
-b --brightness - how bright is it outside on a scale of 1-10
-n --nudge - are you nudging me?
-h --happy - script should be happy.
-s --shaft - what kind of shaft are you?Mixed Command-Line FunctionsIncase you want to be able to do it all, you can call a function with either mixed, positional or both if you like. @expose.mixed
def roll_dice(number_of_faces:int, repetitions:int):
"""
Roll the dice to see if you're lucky or for general D&D pleasure.
number_of_faces - the max value of the die.
repititions - the amount of times to throw the dice.
"""
results = (random.randint(1, number_of_faces) for i in range(repetitions))
for res in results:
print(res, end=' ')Will expose: single.py ?
Usage: single.py -n number_of_faces -r repetitions
Roll the dice to see if you're lucky or for general D&D pleasure.
-n --number_of_faces - the max value of the die.
-r --repititions - the amount of times to throw the dice.
single.py -n 5 -r 2
1 5
single.py 10 6
8 2 1 2 4 1
single.py -r 4 100
73 91 95 62Exposing many functions at onceYou can mix and match positional functions with keyword functions. Whenever more than one command-line function exists, your main script becomes similar to django's manage.py in that in order to access a function you must enter script.py [function_name] [args]. Check it out: @expose.args
def possy(archer:str, boulder:float, magic:int=42):
"""Shows an example positional command-line function.
archer - is a str
boulder - should be a float
magic - a number that is magical"""
print(repr(archer), repr(boulder), repr(magic))
@expose.kwargs
def bigfun(brightness:int, nudge:bool, happy:bool, shaft:str='gold'):
"""
bigfun is a keyword command-line function.
Switches can be given in any order.
Also, booleans can be combined ie: -nh
brightness - how bright is it outside on a scale of 1-10
nudge - are you nudging me?
happy - script should be happy.
shaft - what kind of shaft are you?
"""
if brightness < 10:
print("Always look on the bright side of life.")
print("You are a shaft of %s when all around is dark." % shaft)
if nudge:
print("Wink ;)")
if not happy:
print("Let's bicker and argue over who killed who.")
Gives the following functionailty: Usage: example.py [function_name] [args]
Available functions are:
possy archer boulder [magic]
Shows an example positional command-line function.
archer - is a str
boulder - should be a float
magic - a number that is magical
bigfun -b brightness [-s shaft] [-n] [-h]
bigfun is a keyword command-line function.
Switches can be given in any order.
Also, booleans can be combined ie: -nh
-b --brightness - how bright is it outside on a scale of 1-10
-n --nudge - are you nudging me?
-h --happy - script should be happy.
-s --shaft - what kind of shaft are you?Advanced - Pyopt without decoratorsIf you want to choose which functions are exposed in the same place you run them, here's how to do it: if __name__ == "__main__":
expose = pyopt.Exposer()
expose.mixed(YourFunction)
expose.args(AnotherFunction)
expose.run()Advanced - Make your own typesBecause the type casting is done through annotations, you can make your own types and parsers and simply use them as annotations. All your custom type needs to do is return what should be passed to the function, the argument is going to be a string with the given argument/option. def my_custom_type(text):
return text.upper()
@expose.args
def my_func(bigger:my_custom_type):
# 'bigger' will always be uppercase when using Exposer to invoke it.
print(bigger)
This gives a great deal of flexibility. For example, consider passing a dictionary at the command line: from urllib.request import urlopen
from urllib.parse import urlencode
import ast
@expose.args
def wget(url, data:ast.literal_eval):
print(urlopen(url + urlencode(data)).read())
|