docopt

Option with optional argument

人走茶凉 提交于 2021-01-28 22:13:12
问题 Suppose that I have My program Usage: myprog [options] Options: -h, --help Show this screen. --version Show version. --files=<arg> Files. [default: foo.txt] I would like to distinguish in my code: --files not specified. --files specified, but with no argument to accept the default. --files myfile , i.e. --files specified with custom argument. With the current docstring I can either Not specify --files . Specify --files with an argument. So I'm missing: The option to specify --files without an

Option with optional argument

北城以北 提交于 2021-01-28 22:05:52
问题 Suppose that I have My program Usage: myprog [options] Options: -h, --help Show this screen. --version Show version. --files=<arg> Files. [default: foo.txt] I would like to distinguish in my code: --files not specified. --files specified, but with no argument to accept the default. --files myfile , i.e. --files specified with custom argument. With the current docstring I can either Not specify --files . Specify --files with an argument. So I'm missing: The option to specify --files without an

Python 命令行库的大乱斗

旧城冷巷雨未停 提交于 2020-12-18 17:34:15
当你想实现一个命令行程序时,或许第一个想到的是用 Python 来实现。比如 CentOS 上大名鼎鼎的包管理工具 yum 就是基于 Python 实现的。 而 Python 的世界中有很多命令行库,每个库都各具特色。但我们往往不知道其背后的设计理念,也因此在选择时感到迷茫。这些库的作者为何在重复造轮子,他是从哪个角度来考虑,来让命令行库“演变”到一个新的更好用的形态。 为了能够更加直观地感受到命令行库的设计理念,在此之前,我们不妨设计一个名为 calc 的命令行程序,它能: 支持 echo 子命令,对输入的字符串做处理来输出若不提供任何选项,则输出原始内容若提供 --lower 选项,则输出小写字符串若提供 --upper 选项,则输出大写字符串 支持 eval 子命令,针对输入调用 Python 的 eval 函数,将结果输出(作为示例,我们不考虑安全性问题) argparse argparse 作为 Python 的标准库,可能会是你想到第一个命令行库。 argparse 的设计理念就是提供给开发者最细粒度的控制。换句话说,你需要告诉它必不可少的细节,比如参数的类型是什么,处理参数的动作是怎样的。 在 argparse 的世界中,需要: 设置解析器,作为后续定义参数和解析命令行的基础。如果要实现子命令,则还要设置子解析器。 定义参数,包括名称、类型、动作、帮助等

用 Python 写一个命令行火车票查看器

天涯浪子 提交于 2020-08-05 18:26:59
当你想查询一下火车票信息的时候,你还在上12306官网吗?或是打开你手机里的APP? 下面让我们来用Python写一个命令行版的火车票查看器, 只要在命令行敲一行命令就能获得你想要的火车票信息!如果你刚掌握了Python基础,这将是个不错的小练习。 接口设计 一个应用写出来最终是要给人使用的,哪怕只是给你自己使用。所以,首先应该想想你希望怎么使用它? 让我们先给这个小应用起个名字吧,既然及查询票务信息,那就叫它tickets好了。我们希望用户只要输入出发站,到达站以及日期就让就能获得想要的信息,所以tickets应该这样被使用: $ tickets from to date 另外,火车有各种类型,高铁、动车、特快、快速和直达,我们希望可以提供选项只查询特定的一种或几种的火车,所以,我们应该有下面这些选项: -g 高铁 -d 动车 -t 特快 -k 快速 -z 直达 这几个选项应该能被组合使用,所以,最终我们的接口应该是这个样子的: $ tickets [-gdtkz] from to date 接口已经确定好了,剩下的就是实现它了。 开发环境 写Python程序的一个良好实践是使用virtualenv这个工具建一个虚拟的环境。我们的程序使用Python3开发,下面在你的工作目录下建一个文件夹tickets,进去创建一个虚拟环境: $ virtualenv -p /usr/bin

简单的Python 火车抢票程序

烂漫一生 提交于 2020-04-28 22:24:48
当你想查询一下火车票信息的时候,你还在上12306官网吗?或是打开你手机里的APP?下面让我们来用Python写一个命令行版的火车票查看器, 只要在命令行敲一行命令就能获得你想要的火车票信息!如果你刚掌握了Python基础,这将是个不错的小练习。 接口设计 一个应用写出来最终是要给人使用的,哪怕只是给你自己使用。所以,首先应该想想你希望怎么使用它?让我们先给这个小应用起个名字吧,既然及查询票务信息,那就叫它tickets好了。我们希望用户只要输入出发站,到达站以及日期就让就能获得想要的信息,所以tickets应该这样被使用: $ tickets from to date 另外,火车有各种类型,高铁、动车、特快、快速和直达,我们希望可以提供选项只查询特定的一种或几种的火车,所以,我们应该有下面这些选项: -g 高铁 -d 动车 -t 特快 -k 快速 -z 直达 这几个选项应该能被组合使用,所以,最终我们的接口应该是这个样子的: $ tickets [-gdtkz] from to date 接口已经确定好了,剩下的就是实现它了。 开发环境 写Python程序的一个良好实践是使用virtualenv这个工具建一个虚拟的环境。我们的程序使用Python3开发,下面在你的工作目录下建一个文件夹tickets,进去创建一个虚拟环境: $ virtualenv -p /usr/bin

10个超有趣的Python项目,你会哪个?

让人想犯罪 __ 提交于 2020-04-20 13:37:39
前言: Python可谓是现在很多人正在学或者想学的一个脚本语言了,提到学习自然就少不了拿项目练手,可是一般的项目根本提不起兴趣嘛,这10个项目可是非常有趣的,不信你看看。 【Python 图片转字符画】 用 50 行 Python 代码完成图片转字符画小工具。通过实验将学习到 Linux 命令行操作,Python 基础,pillow 库的使用,argparse 库的使用。 效果图 【使用 Python 生成分形图片】 这里特别注意:不管你是为了Python就业还是兴趣爱好,记住:项目开发经验永远是核心,如果你没有2020最新python入门到高级实战视频教程,可以去小编的Python交流.裙 :七衣衣九七七巴而五(数字的谐音)转换下可以找到了,里面很多新python教程项目,还可以跟老司机交流讨教! 用Python Turtle 模块,通过画出分形树(Fractal tree),科赫雪花曲线(Koch snowflake)和龙形曲线(Dragon curve)这三种曲线来加强对递归的认识。 效果图一 【Python3 色情图片识别】 使用 Python3 去识别图片是否为色情图片,我们会使用到 PIL 这个图像处理库,会编写算法来划分图像的皮肤区域。其中涉及到Python 3 基础知识,肤色像素检测与皮肤区域划分算法,Pillow及argparse的使用。 效果图一

Python - Difference between docopt and argparse

為{幸葍}努か 提交于 2020-01-12 12:09:56
问题 I have to write a command-line interface and I've seen I can use docopt and argparse . I would like to know what are the main differences between the two so that I can make an enlightened choice. Please stick to the facts. I don't want Wow. docopt. So beautiful. Very useful. 回答1: Docopt parses a doc string, whereas argparse constructs its parsing by creating an object instance and adding behaviour to it by function calls. Example for argparse: parser = argparse.ArgumentParser() parser.add

Accepting arbitrary options from docopt

好久不见. 提交于 2020-01-05 04:15:18
问题 Looking through the docopt documentation and examples I can't seem to find this functionality, but I feel as it should exist so I thought I'd ask to make sure. I'm using docopt for Python and want to be able to allow arbitrary options. The use case is a command line templating utility - so arbitrary key values would be handy. """Templator Usage: templator <template> [--arbitrary-option=<value>]... """ Hope that example demonstratives what I'm after. Maybe something like --*=<value> would be

How does the type deduction work in this Docopt example?

回眸只為那壹抹淺笑 提交于 2019-12-24 01:18:00
问题 Take a look at this code using the docopt library: const USAGE: &'static str = "...something..."; #[derive(Deserialize)] struct Args { flag: bool, } type Result<T> = result::Result<T, Box<error::Error + Send + Sync>>; fn main() { let mut args: Args = Docopt::new(USAGE) .and_then(|d| d.deserialize()) .unwrap_or_else(|e| e.exit()); } If you look at the expression to the right of equals sign, you'll see that it doesn't mention the Args struct anywhere. How does the compiler deduce the return

Why are defaults not appearing in my command-line argument dictionary from docopt?

自作多情 提交于 2019-12-22 07:07:10
问题 I've been trying to use docopt to make a simple CLI, but for some reason my default parameters are not appearing. Below is my test code. I am using the latest version of docopt.py from the github repository. """ Usage: scrappy <path> ... [options] -a --auto Automatically scrape and rename without user interaction. -l --lang Specify language code [default: en]. --scan-individual Evaluate series information individually for each file. -c --cfg User alternate config file [default: ../scrappy