https://github.com/digipolitan/command-line-args
https://github.com/digipolitan/command-line-args
command-line-arguments command-line-parser command-line-tool swift
Last synced: 3 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/digipolitan/command-line-args
- Owner: Digipolitan
- License: bsd-3-clause
- Created: 2017-06-19T11:49:23.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2017-11-13T20:31:59.000Z (over 7 years ago)
- Last Synced: 2025-01-17T00:20:16.947Z (4 months ago)
- Topics: command-line-arguments, command-line-parser, command-line-tool, swift
- Language: Swift
- Size: 34.2 KB
- Stars: 0
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
CommandLineArgs
=================================[](https://developer.apple.com/swift/)
[](https://swift.org/package-manager/)
[](http://twitter.com/Digipolitan)Swift library to parse and route command line options
## Installation
### SPM
To install CommandLineArgs with SwiftPackageManager, add the following lines to your `Package.swift`.
```swift
let package = Package(
name: "XXX",
products: [
.library(
name: "XXX",
targets: ["XXX"]),
],
dependencies: [
.package(url: "https://github.com/Digipolitan/command-line-args.git", from: "1.0.0")
],
targets: [
.target(
name: "XXX",
dependencies: ["CommandLineArgs"])
]
)
```## The Basics
### Handle single command
1] Create the command subclass
```swift
public class MyAppCommand: Command {public enum Keys {
public static let help: String = CommandLineArgs.Consts.keys.help
public static let numbers: String = "numbers"
}public enum Options {
public static let help = OptionDefinition(name: Keys.help, type: .boolean, documentation: "Show help banner of specified command")
public static let numbers = OptionDefinition(name: Keys.numbers, type: .int, alias: "n", isMultiple: true)
}public enum Consts {
public static let name: String = "MyAppName"
}public lazy var definition: CommandDefinition = {
return CommandDefinition(name: Consts.name, options: [
Options.help,
Options.numbers
], documentation: "My App command line tool")
}()public func run(_ arguments: [String : Any]) throws {
if let numbers = arguments[Keys.numbers] as? [Int] {
print(numbers) // display numbers value
}
}
}
```2] Register & handle the command inside the CommandLineArgs in your main.swift
```swift
var arguments = CommandLine.arguments
arguments[0] = MyAppCommand.Consts.name // Force the app name inside the first app argumentslet cla = CommandLineArgs()
cla.root(command: MyAppCommand())
cla.handle(arguments)
```3] Execute your command line app as follow:
- Display the help banner
```sh
$ MyAppName --help
```**Output**
```sh
Usage :$ MyAppName [OPTIONS]
My App command line tool
Options :
--help Boolean
Show help banner of specified command--numbers|-n Int[]
```---
- Execute using full arg name
```sh
$ MyAppName --numbers 1 2
```**Output**
```sh
[1, 2]
```---
- Execute using alias
```sh
$ MyAppName -n 1 2 3 4
```**Output**
```sh
[1, 2, 3, 4]
```### Handle node command
```swift
let cla = CommandLineArgs()
let app = cla.root(command: MyAppCommand())
app.add(child: StartCommand()) // CommandDefinition name: "start"
app.add(child: StopCommand()) // CommandDefinition name: "stop"
```* `$ MyAppName --numbers 4 3` will trigger MyAppCommand
* `$ MyAppName start XXX` will trigger StartCommand
* `$ MyAppName stop XXX` will trigger StopCommand## Contributing
See [CONTRIBUTING.md](CONTRIBUTING.md) for more details!
This project adheres to the [Contributor Covenant Code of Conduct](CODE_OF_CONDUCT.md).
By participating, you are expected to uphold this code. Please report
unacceptable behavior to [[email protected]](mailto:[email protected]).## License
CommandLineArgs is licensed under the [BSD 3-Clause license](LICENSE).