【发布时间】:2021-05-30 23:06:10
【问题描述】:
我是业余无线电爱好者 [G6SGA] 不是程序员,但我确实尝试过。 :) 使用python3。我正在尝试执行以下操作,但真的无法理解 - argparse 并最终尝试使用“导入点击”。仍然无法理解,所以我在这里。任何所有(礼貌):) 欢迎提出建议。 我希望---
命令行> python3 scratch.py [未提供选项]
output> "你的默认值被使用并且是:9600 and '/dev/ttyAMA0' "
或
命令行> python3 scratch.py 115200 '/dev/ttyABC123'
output> "你的输入值被使用并且是:115200 and '/dev/ttyAMA0'"
所以命令行将采用 [or NOT] 参数/s。将参数存储到代码中的变量中以供将来使用。 这是我尝试过的一些方法:是的,我接受这是一团糟
#!/usr/bin/env python3
# -*- coding: utf_8 -*-
# ========================
# Include standard modules
# import click
# baud default = 9600
# port default = "/dev/ttyAMA0"
import click
@click.command()
# @click.option('--baud', required = False, default = 9600, help = 'baud rate defaults to: 9600')
# @click.option('--port', required = False, default = '/dev/ttyAMA0', help = 'the port to use defaults to: /dev/ttyAMA0')
@click.option('--item', type=(str, int))
def putitem(item):
click.echo('name=%s id=%d' % item)
def communications():
""" This checks the baud rate and port to use
either the command line supplied item or items.
Or uses the default values
abaud = 9600 # default baud rate
b=abaud
aport = "/dev/ttyAMA0"
p=aport
print(f"abaud = {b} and aport = {p}")
"""
# now I wish to check if there were supplied values
# on the command line
# print(f"Baud supplied {click.option.} port supplied {port}" )
if __name__ == '__main__':
putitem() # communications()
【问题讨论】:
-
我有我需要的答案:chuffed。
标签: python-3.x parsing command-line command