Skip to content

Commit

Permalink
CLI package update 1 (#2019)
Browse files Browse the repository at this point in the history
Features added:
   - string_seq option and arg value type for repeated strings collected into a sequence.
   - Command fullname() to make it easier to match on command name.
   - Checking commands are leaves so that partial commands return syntax errors.

Other changes:
   - Tests for the above.
   - _ValueType is now a trait instead of a union. This makes it easier to delegate type specific work to one place, and makes value type more easily extended.
   - A few other cleanups, like the removal of redundant box modifiers on methods.
  • Loading branch information
cquinn authored and jemc committed Jul 18, 2017
1 parent 6b8163a commit 3fb8eec
Show file tree
Hide file tree
Showing 9 changed files with 726 additions and 181 deletions.
14 changes: 12 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,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 @@ -75,12 +85,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

0 comments on commit 3fb8eec

Please sign in to comment.