跳至主要內容
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?

4563博客

全新的繁體中文 WordPress 網站
  • 首頁
  • 让 argparse 也可以通过修饰器形式设置命令行参数
未分類
27 3 月 2021

让 argparse 也可以通过修饰器形式设置命令行参数

让 argparse 也可以通过修饰器形式设置命令行参数

資深大佬 : ungrown 10

https://github.com/mo-han/mo-han-toolbox/blob/8e2e92d0c1b771155bac463f836da34a94982b29/mylib/ez/argparse.py

单纯就是为了偷懒,形似 Click

#test.py  from mylib.ez.argparse import * from mylib.ez import AttrName  pr = ParserRigger() ro = RawObject an = AttrName()  # an.attribute == 'attribute'   @pr.super_command() @pr.argument(an.x) @pr.argument(an.y) @pr.argument(an.z) @pr.flag(an.l, help='print in multiple lines') @pr.call_map(an.x, ro(1111), an.z, UnknownArguments(), multi_line=an.l) def xyz(x, y, z, more, multi_line=False):     print(f'x={x}', f'y={y}', f'z={z}', 'and more', *more, sep='n' if multi_line else ', ')   an.x = an.y = an.z = an.l = None  pr.parse_known_args() pr.run() 
>test usage: test.py [-h] [-l] x y z test.py: error: the following arguments are required: x, y, z  >test -h usage: test.py [-h] [-l] x y z  positional arguments:   x   y   z  optional arguments:   -h, --help  show this help message and exit   -l          print in multiple lines    >test 0 0 0 a b c x=0, y=1111, z=0, and more, a, b, c  >test 0 0 0 a b c -l x=0 y=1111 z=0 and more a b c 

大佬有話說 (0)

文章導覽

上一篇文章
下一篇文章

AD

其他操作

  • 登入
  • 訂閱網站內容的資訊提供
  • 訂閱留言的資訊提供
  • WordPress.org 台灣繁體中文

51la

4563博客

全新的繁體中文 WordPress 網站
返回頂端
本站採用 WordPress 建置 | 佈景主題採用 GretaThemes 所設計的 Memory
4563博客
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?
在這裡新增小工具