在python中运行脚本程序

时间:2018-08-01 13:40:49

标签: python action argparse shutil

我在python中有一个小项目要做。我必须在程序中解析4个参数。 所以命令是: -i(存储源文件) -d(存储目标文件) -a(存储名为i386,x64_86或all的文件夹) -p(存储名为Linux,Windows或所有文件夹)

Linux文件夹中有2个文件夹:i386和x64_86;该文件夹也有这两个文件夹窗口

我的脚本必须像我告诉他那样复制伪造者,例如9种组合:

Python exemple.py -i -d -a i386 p Windows
因此,在这个示例中,我只需要复制仅包含文件夹i386的前窗即可。

要复制文件,我使用shutil.copytree(source_file,destination,ignore = ignore_patterns(.....))

我设法对输入和输出(args.input,args.output)进行访问,但是对于arch和平台,我必须对coices进行访问,而我不知道该怎么做。

有什么想法吗?

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")

有什么想法吗?

2 个答案:

答案 0 :(得分:0)

add_argument("-i", "--input", required=True, nargs="?")

可以,但是不是最好的。通常带有'?'的标记参数与defaultconst参数一起使用。如果未使用该标志,它将获得default。如果在不添加字符串的情况下使用,它将获得const。否则它将获取字符串。

通过使用required,您可以消除其中一种选择。但是您没有指定const。我会放下nargs。最好还是删除required并定义一个合理的default

我一直鼓励海报制作者

args = pars.parse_args()
print(args)

在尝试使用命名空间之前先看看解析器能为您提供什么。

args.arch
args.platform

每个人都有一个对应于他们选择之一的字符串值。

类似的通用参数将用于:

if args.arch == 'all':
    ....
elif args.arch == "i386":
    ....

if args.arch in ['all', 'i386']:
    # do something with i386
elif     ....  in ['all', "x86_64"]:
    ....

if逻辑只需要满足您的需求。这些是使用此类值的替代方法,例如字典查找

adict[args.arch]

一旦有了args.arch等值,就完成了argparse。其余的就是处理替代方案所需的程序逻辑。

答案 1 :(得分:0)

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)