12-system-resources/08-command-line-options.protos
- Repository
- guillermomolina/protos
- Commit
379c6fa63685c39a5954cfbd577bfda3b4df8e75- Tree
8cd4f4afe29ae4c3866381f38c1858108e9799f6- Blob
eabc8fa4443d6875633575abed67e0e7b7a456bd- Source
- View exact source on GitHub
// THE LICENSED WORK IS PROVIDED UNDER THE TERMS OF THE ADAPTIVE PUBLIC LICENSE
// ("LICENSE") AS FIRST COMPLETED BY: Guillermo Adrián Molina. ANY USE, PUBLIC
// DISPLAY, PUBLIC PERFORMANCE, REPRODUCTION OR DISTRIBUTION OF, OR PREPARATION OF
// DERIVATIVE WORKS BASED ON, THE LICENSED WORK CONSTITUTES RECIPIENT'S ACCEPTANCE
// OF THIS LICENSE AND ITS TERMS, WHETHER OR NOT SUCH RECIPIENT READS THE TERMS OF
// THE LICENSE. "LICENSED WORK" AND "RECIPIENT" ARE DEFINED IN THE LICENSE. A COPY
// OF THE LICENSE IS LOCATED IN THE TEXT FILE ENTITLED "LICENSE.TXT" ACCOMPANYING
// THE CONTENTS OF THIS FILE. IF A COPY OF THE LICENSE DOES NOT ACCOMPANY THIS
// FILE, A COPY OF THE LICENSE MAY ALSO BE OBTAINED AT THE FOLLOWING WEB SITE:
// https://github.com/guillermomolina/protos
//
// Software distributed under the License is distributed on an "AS IS" basis,
// WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
// the specific language governing rights and limitations under the License.
// Command-line specifications with std:cli/command_line.
//
// A specification is ordinary data built by CommandLine.option,
// CommandLine.positional and CommandLine.command. CommandLine.parse checks a
// fixed Array of arguments against it; this lesson never reads the real
// process arguments.
CommandLine: import("std:cli/command_line")
verbose: CommandLine.option(
key: "verbose", long: "verbose", short: "v",
minValues: 0, maxValues: 0,
minOccurrences: 0, maxOccurrences: 1,
valueName: null, help: "Print progress"
)
output: CommandLine.option(
key: "output", long: "output", short: "o",
minValues: 1, maxValues: 1,
minOccurrences: 1, maxOccurrences: 1,
valueName: "FILE", help: "Destination file"
)
inputs: CommandLine.positional(
key: "inputs", minOccurrences: 1, maxOccurrences: null,
valueName: "INPUT", help: "Files to convert"
)
spec: CommandLine.command({
name: "convert"
help: "Convert input files"
options: Array(verbose, output)
positionals: Array(inputs)
subcommands: Array()
})
result: CommandLine.parse(spec, Array("-v", "--output=out.txt", "a.md", "b.md"))
options: result.command.options
print(options.size()) // 2
print(options[0].key) // verbose
print(options[1].key) // output
print(options[1].value) // out.txt
print(result.command.positionals.size()) // 2
print(result.command.positionals[1].value) // b.md
// --output is required (minOccurrences: 1), so omitting it is rejected.
print(Error.handle(
() => { CommandLine.parse(spec, Array("a.md")); "accepted" },
(error) => { "missing --output rejected" }
))
// An unknown option is rejected as well.
print(Error.handle(
() => { CommandLine.parse(spec, Array("--fast", "--output", "x", "a.md")); "accepted" },
(error) => { "unknown option rejected" }
))
// Help text is pure: it is rendered as a String, not written anywhere.
print(CommandLine.renderHelp(spec, Array()))