commander

所属分类:collect
开发工具:Crystal
文件大小:0KB
下载次数:0
上传日期:2021-03-30 07:19:32
上 传 者sh-1993
说明:  Crystal编程语言的命令行界面生成器。,
(Command-line interface builder for the Crystal programming language.,)

文件列表:
CHANGELOG.md (574, 2021-03-30)
LICENSE (1081, 2021-03-30)
shard.yml (124, 2021-03-30)
spec/ (0, 2021-03-30)
spec/commander_spec.cr (22737, 2021-03-30)
spec/flag_spec.cr (1478, 2021-03-30)
spec/flags_spec.cr (949, 2021-03-30)
spec/help_spec.cr (4089, 2021-03-30)
spec/spec_helper.cr (42, 2021-03-30)
src/ (0, 2021-03-30)
src/commander.cr (583, 2021-03-30)
src/commander/ (0, 2021-03-30)
src/commander/command.cr (3876, 2021-03-30)
src/commander/commands.cr (421, 2021-03-30)
src/commander/exception.cr (43, 2021-03-30)
src/commander/flag.cr (3390, 2021-03-30)
src/commander/flags.cr (678, 2021-03-30)
src/commander/options.cr (608, 2021-03-30)
src/commander/parser.cr (1420, 2021-03-30)
src/commander/parser/ (0, 2021-03-30)
src/commander/parser/base.cr (906, 2021-03-30)
src/commander/parser/long_flag_format.cr (1199, 2021-03-30)
src/commander/parser/short_flag_format.cr (1062, 2021-03-30)
src/commander/version.cr (41, 2021-03-30)

## Commander [![Test Status](https://github.com/mrrooijen/commander/workflows/Test/badge.svg)](https://github.com/mrrooijen/commander/actions) Command-line interface builder for the [Crystal] programming language. #### Installation Add this to your application's `shard.yml`: ```yaml dependencies: commander: github: mrrooijen/commander version: ~> 0.4.0 ``` #### Usage Almost everything you need to know in one example. Refer to the [Features](#features) section below for a list of all available features. ```crystal require "commander" cli = Commander::Command.new do |cmd| cmd.use = "my_program" cmd.long = "my program's (long) description." cmd.flags.add do |flag| flag.name = "env" flag.short = "-e" flag.long = "--env" flag.default = "development" flag.description = "The environment to run in." end cmd.flags.add do |flag| flag.name = "port" flag.short = "-p" flag.long = "--port" flag.default = 8080 flag.description = "The port to bind to." end cmd.flags.add do |flag| flag.name = "timeout" flag.short = "-t" flag.long = "--timeout" flag.default = 29.5 flag.description = "The wait time before dropping the connection." end cmd.flags.add do |flag| flag.name = "verbose" flag.short = "-v" flag.long = "--verbose" flag.default = false flag.description = "Enable more verbose logging." flag.persistent = true end cmd.run do |options, arguments| options.string["env"] # => "development" options.int["port"] # => 8080 options.float["timeout"] # => 29.5 options.bool["verbose"] # => false arguments # => Array(String) puts cmd.help # => Render help screen end cmd.commands.add do |cmd| cmd.use = "kill " cmd.short = "Kills server by pid." cmd.long = cmd.short cmd.run do |options, arguments| arguments # => ["62719"] end end end Commander.run(cli, ARGV) ``` Here's what the help page looks like for this configuration: ``` $ my_program help my_program - my program's (long) description. Usage: my_program [command] [flags] [arguments] Commands: help [command] Help about any command. kill Kills server by pid. Flags: -e, --env The environment to run in. default: 'development' -h, --help Help for this command. -p, --port The port to bind to. default: 8080 -t, --timeout The wait time before dropping the connection. default: 29.5 -v, --verbose Enable more verbose logging. ``` This is how you override the default options and pass in additional arguments: ``` $ my_program -ve production --port 8443 --timeout=25 arg1 arg2 arg3 -- arg4 --arg5 ``` ```crystal cmd.run do |options, arguments| options.string["env"] # => "production" options.int["port"] # => 8443 options.float["timeout"] # => 25.0 options.bool["verbose"] # => true arguments # => ["arg1", "arg2", "arg3"] end ``` #### Features - Define commands recursively - Define flags on a per-command basis - Short argument flags (`-p 8080`) - Short boolean flags (`-f`) - Multi-short flags (`-fp 8080`, equivalent to `-f -p 8080`) - Long argument flags (`--port 8080`, `--port=8080`) - Long boolean flags (`--force`) - Share flags with multiple commands (`verbose = Commander::Flag.new`) - Persistent flags for recursively inheriting flags from a parent command (`flag.persistent = true`) - Global flags by defining persistent flags on the root command (`flag.persistent = true`) - Default values for each flag - Automatically validates, parses and casts to the correct type - Automatically passes all parsed `options` to `cmd.run` - Receive additional cli arguments per command (`arguments` in `cmd.run`) - Receive unmapped flags as arguments (`cmd.ignore_unmapped_flags = true`) - Receive all input after `--` as arguments - Automatically generates a help page for each command - Generates a `help` command for each command to access the help page - Generates `-h, --help` flags for each command to access to help page - Provide `Commander.run(cli, ARGV)` to handle end-user input exceptions. #### File Structure and Testing Refer to [this answer](https://github.com/mrrooijen/commander/issues/13#issuecomment-320645899). #### Contributing 1. Fork it ( https://github.com/mrrooijen/commander/fork ) 2. Create your feature branch (git checkout -b my-new-feature) 3. Commit your changes (git commit -am 'Add some feature') 4. Push to the branch (git push origin my-new-feature) 5. Create a new Pull Request [Crystal]: http://crystal-lang.org

近期下载者

相关文件


收藏者