swift-argument-parser开源项目

我要开发同款
匿名用户2021年11月17日
35阅读
开发技术JavaScript
所属分类应用工具、终端/远程登录
授权协议Apache-2.0 License

作品详情

SwiftArgumentParserUsage

Beginbydeclaringatypethatdefinestheinformationthatyouneedtocollectfromthecommandline.DecorateeachstoredpropertywithoneofArgumentParser'spropertywrappers,andthendeclareconformancetoParsableCommandandaddthe@mainattribute.Finally,implementyourcommand'slogicintherun()method.

importArgumentParser@mainstructRepeat:ParsableCommand{@Flag(help:"Includeacounterwitheachrepetition.")varincludeCounter=false@Option(name:.shortAndLong,help:"Thenumberoftimestorepeat'phrase'.")varcount:Int?@Argument(help:"Thephrasetorepeat.")varphrase:Stringmutatingfuncrun()throws{letrepeatCount=count??.maxforiin1...repeatCount{ifincludeCounter{print("\(i):\(phrase)")}else{print(phrase)}}}}

TheArgumentParserlibraryparsesthecommand-linearguments,instantiatesyourcommandtype,andtheneitherexecutesyourrun()methodorexitswithausefulmessage.

ArgumentParserusesyourproperties'namesandtypeinformation,alongwiththedetailsyouprovideusingpropertywrappers,tosupplyusefulerrormessagesanddetailedhelp:

$repeathello--count3hellohellohello$repeat--count3Error:Missingexpectedargument'phrase'.Help:<phrase>Thephrasetorepeat.Usage:repeat[--count<count>][--include-counter]<phrase>See'repeat--help'formoreinformation.$repeat--helpUSAGE:repeat[--count<count>][--include-counter]<phrase>ARGUMENTS:<phrase>Thephrasetorepeat.OPTIONS:--include-counterIncludeacounterwitheachrepetition.-c,--count<count>Thenumberoftimestorepeat'phrase'.-h,--helpShowhelpforthiscommand.

Formoreinformationanddocumentationaboutallsupportedoptions,seethelibrary'sdocumentationinXcode.

Examples

Thisrepositoryincludesafewexamplesofusingthelibrary:

repeatistheexampleshownabove.rollisasimpleutilityimplementedasastraight-linescript.mathisanannotatedexampleofusingnestedcommandsandsubcommands.

YoucanalsoseeexamplesofArgumentParseradoptionamongSwiftprojecttools:

indexstore-dbisasimpleutilitywithtwocommands.swift-formatusessomeadvancedfeatures,likecustomoptionvaluesandhiddenflags.ProjectStatus

TheSwiftArgumentParserpackageissourcestable;versionnumbersfollowsemanticversioning.SourcebreakingchangestopublicAPIcanonlylandinanewmajorversion.

ThepublicAPIofversion1.0oftheswift-argument-parserpackageconsistsofnon-underscoreddeclarationsthataremarkedpublicintheArgumentParsermodule.Interfacesthataren'tpartofthepublicAPImaycontinuetochangeinanyrelease,includingtheexactwordingandformattingoftheautogeneratedhelpanderrormessages,aswellasthepackage’sexamples,tests,utilities,anddocumentation.

Futureminorversionsofthepackagemayintroducechangestotheserulesasneeded.

We'dlikethispackagetoquicklyembraceSwiftlanguageandtoolchainimprovementsthatarerelevanttoitsmandate.Accordingly,fromtimetotime,weexpectthatnewversionsofthispackagewillrequireclientstoupgradetoamorerecentSwifttoolchainrelease.RequiringanewSwiftreleasewillonlyrequireaminorversionbump.

AddingArgumentParserasaDependency

TousetheArgumentParserlibraryinaSwiftPMproject,addittothedependenciesforyourpackageandyourcommand-lineexecutabletarget:

letpackage=Package(//name,platforms,products,etc.dependencies:[//otherdependencies.package(url:"https://github.com/apple/swift-argument-parser",from:"1.0.0"),],targets:[.executableTarget(name:"<command-line-tool>",dependencies:[//otherdependencies.product(name:"ArgumentParser",package:"swift-argument-parser"),]),//othertargets])
声明:本文仅代表作者观点,不代表本站立场。如果侵犯到您的合法权益,请联系我们删除侵权资源!如果遇到资源链接失效,请您通过评论或工单的方式通知管理员。未经允许,不得转载,本站所有资源文章禁止商业使用运营!
下载安装【程序员客栈】APP
实时对接需求、及时收发消息、丰富的开放项目需求、随时随地查看项目状态

评论