Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLI package update 1 #2019

Merged
merged 9 commits into from
Jul 18, 2017
14 changes: 12 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ All notable changes to the Pony compiler and standard library will be documented

### Added

- Add cli package implementing the CLI syntax ([RFC #38](https://github.com/ponylang/rfcs/blob/master/text/0038-cli-format.md))
- Initial ([PR #1897](https://github.com/ponylang/ponyc/pull/1897)) implemented the full RFC and contained:
- Enhanced Posix / GNU program argument syntax.
- Commands and sub-commands.
- Bool, String, I64 and F64 option / arg types.
- Help command and syntax errors with formatted output.
- Update ([PR #2019](https://github.com/ponylang/ponyc/pull/2019)) added:
- String-seq (ReadSeq[String]) types for repeated string options / args.
- Command fullname() to make it easier to match on unique command names.
- Checking that commands are leaves so that partial commands return syntax errors.


### Changed
Expand Down Expand Up @@ -72,12 +82,12 @@ All notable changes to the Pony compiler and standard library will be documented

- Compiler error instead of crash for invalid this-dot reference in a trait. ([PR #1879](https://github.com/ponylang/ponyc/pull/1879))
- Compiler error instead of crash for too few args to constructor in case pattern. ([PR #1880](https://github.com/ponylang/ponyc/pull/1880))
- Pony runtime hashmap bug that resulted in issues [#1483](https://github.com/ponylang/ponyc/issues/1483), [#1781](https://github.com/ponylang/ponyc/issues/1781), and [#1872](https://github.com/ponylang/ponyc/issues/1872). ([PR #1886](https://github.com/ponylang/ponyc/pull/1886))
- Pony runtime hashmap bug that resulted in issues [#1483](https://github.com/ponylang/ponyc/issues/1483), [#1781](https://github.com/ponylang/ponyc/issues/1781), and [#1872](https://github.com/ponylang/ponyc/issues/1872). ([PR #1886](https://github.com/ponylang/ponyc/pull/1886))
- Compiler crash when compiling to a library ([Issue #1881](https://github.com/ponylang/ponyc/issues/1881))([PR #1890](https://github.com/ponylang/ponyc/pull/1890))

### Changed

- TCPConnection.connect_failed, UDPNotify.not_listening, TCPListenNotify.not_listening no longer have default implementation. The programmer is now required to implement error handling or consciously choose to ignore. ([PR #1853](https://github.com/ponylang/ponyc/pull/1853)
- TCPConnection.connect_failed, UDPNotify.not_listening, TCPListenNotify.not_listening no longer have default implementation. The programmer is now required to implement error handling or consciously choose to ignore. ([PR #1853](https://github.com/ponylang/ponyc/pull/1853)

## [0.13.2] - 2017-04-29

Expand Down
69 changes: 29 additions & 40 deletions examples/commandline/main.pony
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,35 @@ use "cli"

actor Main
new create(env: Env) =>
try
let cmd =
match CommandParser(cli_spec()).parse(env.args, env.vars())
| let c: Command => c
| let ch: CommandHelp =>
ch.print_help(env.out)
env.exitcode(0)
return
| let se: SyntaxError =>
env.out.print(se.string())
env.exitcode(1)
return
end
let cs =
try
CommandSpec.leaf("echo", "A sample echo program", [
OptionSpec.bool("upper", "Uppercase words"
where short' = 'U', default' = false)
], [
ArgSpec.string_seq("words", "The words to echo")
]).>add_help()
else
env.exitcode(-1) // some kind of coding error
return
end

// cmd is a valid Command, now use it.
let cmd =
match CommandParser(cs).parse(env.args, env.vars())
| let c: Command => c
| let ch: CommandHelp =>
ch.print_help(env.out)
env.exitcode(0)
return
| let se: SyntaxError =>
env.out.print(se.string())
env.exitcode(1)
return
end

let upper = cmd.option("upper").bool()
let words = cmd.arg("words").string_seq()
for word in words.values() do
env.out.write(if upper then word.upper() else word end + " ")
end

fun tag cli_spec(): CommandSpec box ? =>
"""
Builds and returns the spec for a sample chat client's CLI.
"""
let cs = CommandSpec.parent("chat", "A sample chat program", [
OptionSpec.bool("admin", "Chat as admin" where default' = false)
OptionSpec.string("name", "Your name" where short' = 'n')
OptionSpec.i64("volume", "Chat volume" where short' = 'v')
], [
CommandSpec.leaf("say", "Say something", Array[OptionSpec](), [
ArgSpec.string("words", "The words to say")
])
CommandSpec.leaf("emote", "Send an emotion", [
OptionSpec.f64("speed", "Emote play speed" where default' = F64(1.0))
], [
ArgSpec.string("emotion", "Emote to send")
])
CommandSpec.parent("config", "Configuration commands", Array[OptionSpec](), [
CommandSpec.leaf("server", "Server configuration", Array[OptionSpec](), [
ArgSpec.string("address", "Address of the server")
])
])
])
cs.add_help()
cs
env.out.print("")
Loading