[CRIU] [PATCH yoda] Add command line parsing

Andrew Vagin avagin at parallels.com
Fri May 31 06:53:33 EDT 2013


On Fri, May 31, 2013 at 02:51:42PM +0400, Cyrill Gorcunov wrote:
> 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>
> ---
> Please take a look, I'm far from being python master.
> 
>  yoda.py | 26 ++++++++++++++++++++++----
>  1 file changed, 22 insertions(+), 4 deletions(-)
> 
> diff --git a/yoda.py b/yoda.py
> index 6b54e79..7c117df 100644
> --- a/yoda.py
> +++ b/yoda.py
> @@ -17,6 +17,7 @@
>  #
>  
>  import sys
> +import argparse
>  
>  opt_option = 1
>  opt_argument = 2
> @@ -35,9 +36,26 @@ 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()
> +
> +#
> +# Process optional arguments
> +if arg_opts.dir[-1] != '/':
> +	arg_opts.dir += "/"
> +
> +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 +233,7 @@ yopts.append(yopt)
>  #
>  ##
>  
> -yname = sys.argv[2];
> +yname = arg_opts.name;
>  
>  ctypes = {
>  	typ_boolean:	"bool",
> @@ -264,7 +282,7 @@ def opt_deprecated(yopt):
>  # Generate the .h file
>  #
>  
> -yinfile = open("yopts.h.in")
> +yinfile = open(arg_opts.dir + "yopts.h.in")
>  yincode = yinfile.read()
>  yincode = yincode.replace("${PROJ}", yname)
>  
> @@ -308,7 +326,7 @@ youtfile.close()
>  #
>  
>  # Get the template in
> -yinfile = open("yopts.c.in")
> +yinfile = open(arg_opts.dir + "yopts.c.in")

os.path.join(arg_opts.dir, "yopts.c.in")

>  yincode = yinfile.read()
>  yincode = yincode.replace("${PROJ}", yname)
>  
> -- 
> 1.8.1.4
> 


More information about the CRIU mailing list