swift-argument-parser

授权协议 Apache-2.0 License
开发语言 JavaScript
所属分类 应用工具、 终端/远程登录
软件类型 开源软件
地区 不详
投 递 者 杨志强
操作系统 跨平台
开源组织
适用人群 未知
 软件概览

Swift Argument Parser

Usage

Begin by declaring a type that defines the informationthat you need to collect from the command line.Decorate each stored property with one of ArgumentParser's property wrappers,and then declare conformance to ParsableCommand and add the @main attribute.Finally, implement your command's logic in the run() method.

import ArgumentParser

@main
struct Repeat: ParsableCommand {
    @Flag(help: "Include a counter with each repetition.")
    var includeCounter = false

    @Option(name: .shortAndLong, help: "The number of times to repeat 'phrase'.")
    var count: Int?

    @Argument(help: "The phrase to repeat.")
    var phrase: String

    mutating func run() throws {
        let repeatCount = count ?? .max

        for i in 1...repeatCount {
            if includeCounter {
                print("\(i): \(phrase)")
            } else {
                print(phrase)
            }
        }
    }
}

The ArgumentParser library parses the command-line arguments,instantiates your command type, and then either executes your run() methodor exits with a useful message.

ArgumentParser uses your properties' names and type information,along with the details you provide using property wrappers,to supply useful error messages and detailed help:

$ repeat hello --count 3
hello
hello
hello
$ repeat --count 3
Error: Missing expected argument 'phrase'.
Help:  <phrase>  The phrase to repeat.
Usage: repeat [--count <count>] [--include-counter] <phrase>
  See 'repeat --help' for more information.
$ repeat --help
USAGE: repeat [--count <count>] [--include-counter] <phrase>

ARGUMENTS:
  <phrase>                The phrase to repeat.

OPTIONS:
  --include-counter       Include a counter with each repetition.
  -c, --count <count>     The number of times to repeat 'phrase'.
  -h, --help              Show help for this command.

For more information and documentation about all supported options,see the library's documentation in Xcode.

Examples

This repository includes a few examples of using the library:

  • repeat is the example shown above.
  • roll is a simple utility implemented as a straight-line script.
  • math is an annotated example of using nested commands and subcommands.

You can also see examples of ArgumentParser adoption among Swift project tools:

  • indexstore-db is a simple utility with two commands.
  • swift-format uses some advanced features, like custom option values and hidden flags.

Project Status

The Swift Argument Parser package is source stable;version numbers follow semantic versioning.Source breaking changes to public API can only land in a new major version.

The public API of version 1.0 of the swift-argument-parser packageconsists of non-underscored declarations that are marked public in the ArgumentParser module.Interfaces that aren't part of the public API may continue to change in any release,including the exact wording and formatting of the autogenerated help and error messages,as well as the package’s examples, tests, utilities, and documentation.

Future minor versions of the package may introduce changes to these rules as needed.

We'd like this package to quickly embrace Swift language and toolchain improvements that are relevant to its mandate.Accordingly, from time to time,we expect that new versions of this package will require clients to upgrade to a more recent Swift toolchain release.Requiring a new Swift release will only require a minor version bump.

Adding ArgumentParser as a Dependency

To use the ArgumentParser library in a SwiftPM project,add it to the dependencies for your package and your command-line executable target:

let package = Package(
    // name, platforms, products, etc.
    dependencies: [
        // other dependencies
        .package(url: "https://github.com/apple/swift-argument-parser", from: "1.0.0"),
    ],
    targets: [
        .executableTarget(name: "<command-line-tool>", dependencies: [
            // other dependencies
            .product(name: "ArgumentParser", package: "swift-argument-parser"),
        ]),
        // other targets
    ]
)
  • 看这个小demo就能弄懂到底什么是parser.add_argument()了: 这个demo一共四步: import argparse #1.创建一个ArgumentParser类的对象a a=argparse.ArgumentParser(description="Test") #description里面的字符串内容可以随便填,就是描述你这个对象ArgumentParser类的对象a是用来

  • parser.add_argument用法 detect.py文件: if __name__ == '__main__':    parser = argparse.ArgumentParser()    parser.add_argument('--cfg', type=str, default='cfg/yolov3-spp-6cls.cfg', help='*.cfg path')  

  • 最近开始读论文代码了,遇到一个。名字叫option的py文件,打开一看,清一色的parser.add_argument(),看得是一脸懵逼。。。。十脸懵逼。仔细看了一遍,知道里面大概是一些网络的参数和设置。查了查别人的博客,看到有对语句的详解,但是还是看。不。懂。于是建了个测试的小程序,不如直接看程序结果来的更直观一点。以下是代码及其结果,很简单。 # 代码 import argparse pa

  • Python解析参数 import argparse parser = argparse.ArgumentParser(description = 'TEST') parser.add_argument ('--start-epoch', default = 0, type = int, help = 'please input number') def main(): global

  • parser.add_argument 是 Python 中的 argparse 库中的一个方法,它的作用是向命令行程序的参数解析器添加参数。 常用的参数有: dest:将该参数存储为命名属性。 type:该参数的数据类型。 default:该参数的默认值。 help:该参数的帮助信息。 required:该参数是否为必需参数。 choices:该参数的可选值列表。 metavar:在帮助信息中显

  • 第一种情况:第一个参数不能重复 def add_arguments(self, parser): parser.add_argument('-d', '--from-date', type=str, help='Some help text') parser.add_argument('-d', '--to-date', type=str, help='Some help text

  • 有一个比较有意思的传参方式: 比如在 demo1.py 中指定 action=’store_true’的时候: parser.add_argument(‘–is_train’, action=’store_true’, default=False) 在运行的时候: python demo1.py 默认是False python demo1.py –is_train 是True, 注意这里没有给 i

  • swfit 4 的新特性 今天编译代码发现了这个问题,查了下,修正很简单在相关类前加: @objcMembers class JKWLoginVC: UIViewController { } 或者在方法名前做修改 @objc func clickLoginBtn() { } As of Swift 4 you’ll start seeing the error “Argument of

  • 原文地址: https://blog.csdn.net/charles91/article/details/50604915 1 “…” 和 “..” //0...5是一个闭区间[0,5] for index in 0...5 { print(index)//print "012345" } println("\n") //0..<5是一个前闭后开区间[0,5) for

  • parser = argparse.ArgumentParser() parser.add_argument('--path', '--checkpoints', type=str, default='./checkpoints', help='model checkpoints path (default: ./checkpoints)') parser.add_argument('--mode

  • 创建解析器,ArgumentParser()里面有具体的参数 parser = argparse.ArgumentParser() 添加参数,定义程序需要的参数以及其默认值 ‘’‘名字,type参数类型,default默认值,help:参数描述,metavar’’’ parser.add_argument(’-input’, default=r’test_images/19.jpg’, help=

  • argparse模块 .add_argument 示例 1.作用 ​ 是python用于解析命令行参数和选项的标准模块 2.使用步骤 1:import argparse 2:parser = argparse.ArgumentParser() 3:parser.add_argument() 4:parser.parse_args() 解释:首先导入该模块;然后创建一个解析对象;然后向该对象中添加你

  • argparse 是一个Python的内建模块,用法可以参考官方文档:https://docs.python.org/3/library/argparse.html 主要步骤: import argparse parser = argparse.ArgumentParser() parser.add_argument('--weights', type=str, default='best.pt

  • 定义argparse对象 当一个程序使用了argparse,例如main.py,你可以使用python main.py -h来查看各个参数的使用介绍 class argparse.ArgumentParser(prog=None, usage=None, description=None, epilog=None, parents=[], formatter_class=argparse.Help

  • parser.add_argument("--train_mode",type=str ,choices= ["flare_free" , "flare"] , required=True , help="Train either 'flare' or 'flare  直接运行程序出现下面error train.py: error: the following arguments are requ

  • description是一个描述信息,ArgumentDefaultsHelpFormatter自动添加默认的值的信息到每一个帮助信息的参数中。第一段代码用了ArgumentDefaultsHelpFormatter,打印出了default默认值,第二段代码没用并没有打印出默认值。 #第一段代码 parser = argparse.ArgumentParser(formatter_class=a

  • parser.add_argument('--no-cuda', action='store_true', default=False, help='Disable cuda for training') 有了 action 这一参数,就相当于把 --no-cuda 参数设成了一个“开关”。我们是不需要给这个开关传递具体的值的,只需要“拨一下”这个开关就行

  • parser.add_argument(),里面的参数,如果有–,表示是可选参数,没有–的话,意味着是必选参数,在运行时必须输入,default是没有用的。 其它的基本解释可以参考python add_argument()用法解析 def main(): parser = ArgumentParser() parser.add_argument('--img', default='

 相关资料
  • argument是javascript中函数的一个特殊参数,例如下文,利用argument访问函数参数,判断函数是否执行 <script type="text/javascript"> function sayHello () { if (arguments[0] == "bye") return; else alert( "hello" + arguments[0]

  • swift-parser-generator 是试验性的 Swift 解析器生成器。 swift parser generator 的代码包括尝试使用 Swift 制作类似 Scala 解析器关系选择器,部分可以成功制作简单的解析器,但是还未实现 Packrat 样式解析器。 支持以下操作符解析: // "a" followed by "b"let rule = "a" ~ "b"// "a" o

  • 1、在XCode 资源中是有此图片信息的: 2、但是Xcode调用Assets资源报错:

  • Swift开发中, Swift module 和 Swift Package Manager module的区别是什么? 我们如何寻找他们呢?

  • 问题内容: 我以编程方式在一个iOS Swift项目中有多个视图控制器。我没有情节提要,如果可能的话,我希望避免使用它们。有没有一种方法可以切换到另一个文件(我们将其称为)并使它成为按钮调用的函数的一部分? 我尝试了以下方法: 上面的适用于情节提要,但我想再调用另一个。能做到吗? 问题答案: 试试这个: 使用Swift3:

  • Swift 4中的类是灵活构造的构建块。 与常量,变量和函数类似,用户可以定义类的属性和方法。 Swift 4提供了一些功能,在声明类时,用户无需创建接口或实现文件。 Swift 4允许将类创建为单个文件,并且当初始化类后,默认情况下将创建外部接口。 使用类的好处 - 继承并获取另一个类的属性 类型转换使用户可以在运行时检查类类型 去初始化器负责释放内存资源 引用计数允许类实例具有多个引用 类和结