I have a little project in python to do. I have to parse 4 arguments in my program. so the commands are: -i (store the source_file) -d (store the destination_file) -a (store the a folder named: i386, x64_86 or all ) -p (store the folder named: Linux, Windows or all)
The folder Linux has 2 folders in: i386 and x64_86; the folder has those 2 folderswindows too
My script has to copy the forders like i tell him, there are 9 combinations, for example:
Python exemple.py -i -d -a i386 p windows
So in this exemple i have to copy just the forder windows containing just the folder i386
to copy the files i use the shutil.copytree(source_file, destination, ignore=ignore_patterns(.....))
i manage to acces the input and the output( args.input, args.output) but for arch and platform i have to acces the coices and i dont know how.
Any idea please ?
pars = argparse.ArgumentParser(prog='copy dirs script')
a1 = pars.add_argument("-i", "--input", required=True, nargs="?",
help="the source dirctory is /""X:/.......")
a2 = pars.add_argument("-o", "--output", required=True, nargs="?",
help="the destination dirctory is the curently working dirctory")
pars.add_argument("-a", "--arch", choices=["all", "i386", "x86_64"], required=True,
help="Targeted check architecture: 32b, 64b, All")
pars.add_argument("-p", "--platform", choices=["all", "windows", "linux"], required=True,
help="Targeted check platform: Windows, Linux, All")
Any idea please ?
add_argument("-i", "--input", required=True, nargs="?")
works, but isn't the best. Normally a flagged argument with '?' used with a default
and const
parameters. If the flag isn't used, it gets the default
. If used without added string, it gets the const
. Otherwise it gets the string.
By using required
you eliminate one of those alternatives. But you didn't specify a const
. I'd drop the nargs
. Better yet drop the required
and define a reasonable default
.
I keep encouraging posters to do
args = pars.parse_args()
print(args)
to see what the parser gives you, before you stumble around trying to the Namespace.
args.arch
args.platform
will each have a string value the corresponds to one of their choices.
Commonly arguments like that will be used with:
if args.arch == 'all':
....
elif args.arch == "i386":
....
or
if args.arch in ['all', 'i386']:
# do something with i386
elif .... in ['all', "x86_64"]:
....
The if
logic just has to fit your needs. The are alternative ways of using such values, such as a dictionary look up
adict[args.arch]
Once you have the values, args.arch
, etc, you are done with argparse
. The rest is the same sort of program logic you need to handle alternatives.
import os
from shutil import *
import argparse
var1 = ""
var2 = ""
source_file = ""
destination_file = ""
pars = argparse.ArgumentParser(prog='copy dirs script', description="à copier MSRE localment:",
epilog="Comme ça on copie les repertoires")
pars.add_argument("-i", "--input", nargs="?", type = lambda s : s.lower(),
help="the source dirctory is /""X:/......")
pars.add_argument("-o", "--output", nargs="?", type = lambda s : s.lower(),
help="the destination dirctory is the curently working dirctory")
pars.add_argument("-a", "--arch", choices=("all", "i386", "x86_64"), type = lambda s : s.lower(),
help="Targeted check architecture: 32b, 64b, All")
pars.add_argument("-p", "--platform", choices=("all", "windows", "linux"), type = lambda s : s.lower(),
help="Targeted check platform: Windows, Linux, All")
args = pars.parse_args()
print(args)
print('\n')
if str(args.input) and str(args.output):
source_file = "X:/asdasd/askkkkkkk"
destination_file = os.path.join(os.getcwd(), "Bla/bla/bla")
else:
print("wrog command !!! Please follow the help commands")
if args.arch == 'all' and args.platform == 'all':
var1 = ''
var2 = ''
elif args.arch == 'all' and args.platform == 'linux':
var1 = ''
var2 = 'windows'
elif args.arch == 'all' and args.platform == 'windows':
var1 = ''
var2 = 'linux'
elif args.arch == 'i386' and args.platform == 'all':
var1 = 'x86_64'
var2 = ''
elif args.arch == 'i386' and args.platform == 'linux':
var1 = 'x86_64'
var2 = 'windows'
elif args.arch == 'i386' and args.platform == 'windows':
var1 = 'x86_64'
var2 = 'linux'
elif args.arch == 'x86_64' and args.platform == 'all':
var1 = 'i386'
var2 = ''
elif args.arch == 'x86_64' and args.platform == 'linux':
var1 = 'i386'
var2 = 'windows'
elif args.arch == 'x86_64' and args.platform == 'windows':
var1 = 'i386'
var2 = 'linux'
else:
print("an error has occurred"+ pars.print_help())
ignoreP =ignore_patterns(
"conf", "inc", "java", "VERSION", "*.ini", "*.ksh",
"*.txt", var1, var2)
# print(str(ignoreP))
copytree(source_file, destination_file, ignore=ignoreP)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.