我在我的程序中使用 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
最佳答案
我有同样的问题(在不同的 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
关于python - Argparse 确认,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28462962/