Argparse 确认
Argeparse confirmation
我在我的程序中使用 aprgeparse
创建一个参数,该参数允许用户从 amazon s3 存储桶中删除文件。我是这样创建的:
parser.add_argument("-r", "--remove",type = str, help= "Delete a file (enter full path to file)" , default = '' )
然后我检查文件是否可用,如果是我删除它:
if args.remove:
b = Bucket(conn,default_bucket)
k = Key(b)
if b.get_key(args.remove):
b.delete_key(args.remove)
print ( ' {0} Has been removed'.format(args.remove.split("/")[-1]) )
else:
print ( " No files named '{0}' found ".format(args.remove.split("/")[-1] ) )
print ( ' Please check the bucket path or file name. Use -l command to list files')
我想知道是否有办法提示用户是否真的想让用户删除文件,删除文件通常就是这种情况(每个程序都会这样做)。像这样
>python myprog.py -r foo.txt
>Are your sure [Y/N] : Y
>File deleted
从阅读 docs 开始,当您看到该参数时,我会使用 subclass argparse.Action 的能力来执行您的自定义行为。我将在文档中包含一个稍微修改过的代码版本供您使用。
class CheckDeleteAction(argparse.Action):
def __init__(self, option_strings, dest, nargs=None, **kwargs):
if nargs is not None:
raise ValueError("nargs not allowed")
super(CheckDeleteAction, self).__init__(option_strings, dest, **kwargs)
def __call__(self, parser, namespace, values, option_string):
# put your prompting and file deletion logic here
pass
添加此 parser.add_argument("-r", "--remove", type=str, help="msg", default='', action=CheckDeleteAction)
以利用 class。
我遇到了同样的问题(在不同的 CLI 中)。我建议您使用给定的答案 here。看看詹姆斯给出的答案。它相当容易理解。像这样使用它:
if args.remove:
b = Bucket(conn,default_bucket)
k = Key(b)
if b.get_key(args.remove):
if yes_no('Are you sure: ')
b.delete_key(args.remove)
print ( ' {0} Has been removed'.format(args.remove.split("/")[-1]) )
else:
print ( " No files named '{0}' found ".format(args.remove.split("/")[-1] ) )
print ( ' Please check the bucket path or file name. Use -l command to list
我在我的程序中使用 aprgeparse
创建一个参数,该参数允许用户从 amazon s3 存储桶中删除文件。我是这样创建的:
parser.add_argument("-r", "--remove",type = str, help= "Delete a file (enter full path to file)" , default = '' )
然后我检查文件是否可用,如果是我删除它:
if args.remove:
b = Bucket(conn,default_bucket)
k = Key(b)
if b.get_key(args.remove):
b.delete_key(args.remove)
print ( ' {0} Has been removed'.format(args.remove.split("/")[-1]) )
else:
print ( " No files named '{0}' found ".format(args.remove.split("/")[-1] ) )
print ( ' Please check the bucket path or file name. Use -l command to list files')
我想知道是否有办法提示用户是否真的想让用户删除文件,删除文件通常就是这种情况(每个程序都会这样做)。像这样
>python myprog.py -r foo.txt
>Are your sure [Y/N] : Y
>File deleted
从阅读 docs 开始,当您看到该参数时,我会使用 subclass argparse.Action 的能力来执行您的自定义行为。我将在文档中包含一个稍微修改过的代码版本供您使用。
class CheckDeleteAction(argparse.Action):
def __init__(self, option_strings, dest, nargs=None, **kwargs):
if nargs is not None:
raise ValueError("nargs not allowed")
super(CheckDeleteAction, self).__init__(option_strings, dest, **kwargs)
def __call__(self, parser, namespace, values, option_string):
# put your prompting and file deletion logic here
pass
添加此 parser.add_argument("-r", "--remove", type=str, help="msg", default='', action=CheckDeleteAction)
以利用 class。
我遇到了同样的问题(在不同的 CLI 中)。我建议您使用给定的答案 here。看看詹姆斯给出的答案。它相当容易理解。像这样使用它:
if args.remove:
b = Bucket(conn,default_bucket)
k = Key(b)
if b.get_key(args.remove):
if yes_no('Are you sure: ')
b.delete_key(args.remove)
print ( ' {0} Has been removed'.format(args.remove.split("/")[-1]) )
else:
print ( " No files named '{0}' found ".format(args.remove.split("/")[-1] ) )
print ( ' Please check the bucket path or file name. Use -l command to list