[PATCH] Add command line parsing

Cyrill Gorcunov gorcunov at gmail.com
Fri May 31 06:29:40 EDT 2013


Here we introduce command line parsing with help output.

| usage: yoda.py [-h] -f FILE -n NAME [-d DIR]
|
| optional arguments:
|  -h, --help            show this help message and exit
|  -f FILE, --file FILE  yoda file to parse
|  -n NAME, --name NAME  project name
|  -d DIR, --dir DIR     templates directory

Signed-off-by: Cyrill Gorcunov <gorcunov at gmail.com>
---
 yoda.py | 22 ++++++++++++++++++----
 1 file changed, 18 insertions(+), 4 deletions(-)

diff --git a/yoda.py b/yoda.py
index 6b54e79..7b46c66 100644
--- a/yoda.py
+++ b/yoda.py
@@ -17,6 +17,8 @@
 #
 
 import sys
+import os
+import argparse
 
 opt_option = 1
 opt_argument = 2
@@ -35,9 +37,21 @@ class yoption:
 class ychoice:
 	pass
 
+#
+# Our own command line
+arg_parser = argparse.ArgumentParser()
+arg_parser.add_argument("-f", "--file", required=True, help="yoda file to parse", type=str)
+arg_parser.add_argument("-n", "--name", required=True, help="project name", type=str)
+arg_parser.add_argument("-d", "--dir", default="./", help="templates directory", type=str)
+arg_opts = arg_parser.parse_args()
+
+if arg_opts.file == None or arg_opts.name == None:
+	arg_parser.print_help()
+	sys.exit(1)
+
 # Read yoda file in
 
-yfile = open(sys.argv[1])
+yfile = open(arg_opts.file)
 
 yopts = []
 yopt_name_len_max = 0
@@ -215,7 +229,7 @@ yopts.append(yopt)
 #
 ##
 
-yname = sys.argv[2];
+yname = arg_opts.name;
 
 ctypes = {
 	typ_boolean:	"bool",
@@ -264,7 +278,7 @@ def opt_deprecated(yopt):
 # Generate the .h file
 #
 
-yinfile = open("yopts.h.in")
+yinfile = os.path.join(arg_opts.dir, "yopts.h.in")
 yincode = yinfile.read()
 yincode = yincode.replace("${PROJ}", yname)
 
@@ -308,7 +322,7 @@ youtfile.close()
 #
 
 # Get the template in
-yinfile = open("yopts.c.in")
+yinfile = os.path.join(arg_opts.dir, "yopts.c.in")
 yincode = yinfile.read()
 yincode = yincode.replace("${PROJ}", yname)
 
-- 
1.8.1.4


--Dzs2zDY0zgkG72+7--


More information about the CRIU mailing list