Permalink
Browse files

A new basic example

  • Loading branch information...
1 parent f0eee5e commit 1cd4bde938bd4e4d72afa289d44c47dbfbb4d0b1 Dusty Phillips committed Dec 2, 2012
Showing with 44 additions and 0 deletions.
  1. +44 −0 examples/basic.py
View
@@ -0,0 +1,44 @@
+'''So you want a quick and dirty command line app without screwing around
+with optparse or getopt, but also without a complicated if-else on the length
+of sys.argv. You don't really need a comprehensive help file, cause it's just
+you running the script and knowing what options are available is enough.
+How many boilerplate lines of code is it gonna take?'''
+
+from opterator import opterate # 1
+
+
+@opterate # 2
+def main(filename, color='red', verbose=False): # 3
+ print(filename, color, verbose)
+
+main() # 4
+
+''' Answer: 4 lines.
+
+You get a program that you can call on the command line like so:
+
+ $ python examples/basic.py this_file
+ this_file red False
+
+or so:
+
+ python examples/basic.py this_file --color=blue
+ this_file blue False
+
+or even so:
+
+ $ python examples/basic.py --color=purple another_file --verbose
+ another_file purple True
+
+And you get a not too useless helpfile:
+
+ $ python examples/basic.py -h
+ Usage: basic.py [options] filename
+
+
+
+Options:
+ -h, --help show this help message and exit
+ -c COLOR, --color=COLOR
+ -v, --verbose
+'''

0 comments on commit 1cd4bde

Please sign in to comment.