简体   繁体   English

如何在 Ubuntu 中创建新的命令行标志

[英]How do I create new command line flags in Ubuntu

I have files saved into a "file1.ors" format.我将文件保存为“file1.ors”格式。 In this case, they are similar to dictionary entries.在这种情况下,它们类似于字典条目。 I have been tasked with now converting them to other formats, such as HTML, Bibtext, or JSON.我现在的任务是将它们转换为其他格式,例如 HTML、Bibtext 或 JSON。 However, my supervisor wants me to use python from the command line, and he wants the choice to be decided with new command-line flags.但是,我的主管希望我从命令行使用 python,并且他希望使用新的命令行标志来决定选择。 For example,例如,

$ orsconvert.py --json file1.ors

which would take file1.ors and convert it into a JSON format.这将采用 file1.ors 并将其转换为 JSON 格式。 The issue is that I cannot even find anything on how to create/define my own command line flags that I can use with the program.问题是我什至找不到任何关于如何创建/定义我可以与程序一起使用的命令行标志的信息。 Any suggestions on how to do this?关于如何做到这一点的任何建议?

Use library argparser , this lib comes pre-installed with Python and very userful for creating command line options.使用库argparser ,这个库预装了 Python,对于创建命令行选项非常有用。 Also, will provide a leverage to use -h command to get all the necessay help (check the first example in the doc).此外,还将提供使用-h命令获取所有必要帮助的杠杆作用(查看文档中的第一个示例)。

作为内置argparse库的替代方案——根据我的经验,它使用起来很奇怪,尤其是当你想要实现一个复杂的命令行界面时——你可能还想看看Typer ,它使用 Python 的新类型注释功能和比较容易掌握。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM