gpt4 book ai didi

python - 使用 argparse 和 .profile 的自定义终端命令

转载 作者:太空宇宙 更新时间:2023-11-03 18:33:02 25 4
gpt4 key购买 nike

我正在尝试为 dft.ba URL shortener 创建一个命令行界面使用 python 的 argparse 和 .profile 中调用我的 python 脚本的函数。这是我的 python 脚本中代码的业务端:

parser = argparse.ArgumentParser(description='Shortens URLs with dft.ba')
parser.add_argument('LongURL',help='Custom string at the end of the shortened URL')
parser.add_argument('--source','-s',help='Custom string at the end of the shortened URL')
parser.add_argument('-c','--copy', action="store_true", default=False, help='Copy the shortened URL to the clipboard')
parser.add_argument('-q','--quiet', action="store_true", default=False, help="Execute without printing")
args = parser.parse_args()
target_url=args.LongURL
source=args.source
print source


query_params={'auth':'ip','target':target_url,'format':'json','src':source}
shorten='http://dft.ba/api/shorten'
response=requests.get(shorten,params=query_params)
data=json.loads(response.content)
shortened_url=data['response']['URL']
print data
print args.quiet
print args.copy

if data['error']:
print 'Error:',data['errorinfo']['extended']
elif not args.copy:
if args.quiet:
print "Error: Can't execute quietly without copy flag"
print 'Link:',shortened_url
else:
if not args.quiet:
print 'Link:',shortened_url
print 'Link copied to clipboard'
setClipboardData(shortened_url)

然后在 .profile 中我有这个:

dftba(){
cd ~/documents/scripts
python dftba.py "$1"
}

运行 dftba SomeURL 会向我返回一个缩短的 URL,但所有选项都不起作用。当我尝试在 LongURL 之前使用 -s SomeSource 时,它会给出错误:参数 --source/-s:需要一个参数,之后使用时它什么也不做,当省略时给出错误:参数太少-c-q 由于某种原因给出错误:参数太少。但是,如果我强制复制,我正在使用的复制到剪贴板功能就可以正常工作。

我非常摸索着解决这个问题,所以如果我犯了一些明显的错误,我很抱歉。我感觉问题出在我的 bash 脚本中,我只是不知道出在哪里。

任何帮助将不胜感激。谢谢。

最佳答案

让我们关注解析器的作用

parser = argparse.ArgumentParser(description='Shortens URLs with dft.ba')
parser.add_argument('LongURL',help='Custom string at the end of the shortened URL')
parser.add_argument('--source','-s',help='Custom string at the end of the shortened URL')
parser.add_argument('-c','--copy', action="store_true", default=False, help='Copy the shortened URL to the clipboard')
parser.add_argument('-q','--quiet', action="store_true", default=False, help="Execute without printing")
args = parser.parse_args()
print args # add to debug the `argparse` behavior

LongURL 是始终需要的位置参数。如果缺少,您将收到“参数太少”错误消息。

source 是可选的,但提供时必须包含参数。如果没有给出,args.source 为 None。正如所写,source 参数必须在 LongURL 参数之外给出。

args.copy 和 args.quiet 都是 bool 值;默认值为False;和 True(如果给出)。 (不需要 default=False 参数。)

我还没有尝试使用copyquiet来完成逻辑。如果 LongURLsource 之前出现问题,这将不会发挥作用。

比较这些示例:

In [38]: parser.parse_args('one'.split())
Out[38]: Namespace(LongURL='one', copy=False, quiet=False, source=None)

In [41]: parser.parse_args('-s one two -c -q'.split())
Out[41]: Namespace(LongURL='two', copy=True, quiet=True, source='one')

查看 parse_args 正在解析的内容也可能有所帮助:sys.argv[1:](如果您对从 .profile 中获得的内容有疑问) .

关于python - 使用 argparse 和 .profile 的自定义终端命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22104716/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com