-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prepares for a v2.
- Loading branch information
Showing
8 changed files
with
270 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Copyright (C) 2024 Toitware ApS. All rights reserved. | ||
// Use of this source code is governed by an MIT-style license that can be | ||
// found in the package's LICENSE file. | ||
import cli show * | ||
import host.file | ||
|
||
store-bytes cli/Cli: | ||
cache := cli.cache | ||
|
||
data := cache.get "my-key": | store/FileStore | | ||
// Block that is called when the key is not found. | ||
// The returned data is stored in the cache. | ||
print "Data is not cached. Computing it." | ||
store.save #[0x01, 0x02, 0x03] | ||
|
||
print data // Prints #[0x01, 0x02, 0x03]. | ||
store-directory cli/Cli: | ||
cache := cli.cache | ||
|
||
directory := cache.get-directory-path "my-dir-key": | store/DirectoryStore | | ||
// Block that is called when the key is not found. | ||
// The returned directory is stored in the cache. | ||
print "Directory is not cached. Computing it." | ||
store.with-tmp-directory: | tmp-dir | | ||
// Create a few files with some data. | ||
file.write-content --path="$tmp-dir/data1.txt" "Hello world" | ||
file.write-content --path="$tmp-dir/data2.txt" "Bonjour monde" | ||
store.move tmp-dir | ||
|
||
print directory // Prints the path to the directory. | ||
main args: | ||
// Uses the application name "cli-example" which will be used | ||
// to compute the path of the cache directory. | ||
root-cmd := Command "cli-example" | ||
--help=""" | ||
An example application demonstrating the file-cache. | ||
""" | ||
--options=[ | ||
OptionEnum "mode" ["file", "directory"] | ||
--help="Store a file in the cache." | ||
--required, | ||
] | ||
--run=:: run it | ||
root-cmd.run args | ||
|
||
run invocation/Invocation: | ||
if invocation["mode"] == "file": | ||
store-bytes invocation.cli | ||
else: | ||
store-directory invocation.cli |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Copyright (C) 2024 Toitware ApS. All rights reserved. | ||
// Use of this source code is governed by an MIT-style license that can be | ||
// found in the package's LICENSE file. | ||
import cli show * | ||
|
||
config-example cli/Cli: | ||
config := cli.config | ||
|
||
print "old value: $(config.get "my-key")" | ||
|
||
config["my-key"] = "my-value" | ||
config.write | ||
|
||
dotted-example cli/Cli: | ||
config := cli.config | ||
|
||
print "old value: $(config.get "super-key.sub-key")" | ||
|
||
config["super-key.sub-key"] = "my-value" | ||
config.write | ||
|
||
main args: | ||
// Uses the application name "cli-example" which will be used | ||
// to compute the path of the config file. | ||
root-cmd := Command "cli-example" | ||
--help=""" | ||
An example application demonstrating configurations. | ||
""" | ||
--run=:: run it | ||
root-cmd.run args | ||
|
||
run invocation/Invocation: | ||
config-example invocation.cli | ||
dotted-example invocation.cli |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// Copyright (C) 2024 Toitware ApS. All rights reserved. | ||
// Use of this source code is governed by an MIT-style license that can be | ||
// found in the package's LICENSE file. | ||
import cli show * | ||
|
||
some-chatty-method cli/Cli: | ||
ui := cli.ui | ||
ui.debug "This is a debug message." | ||
ui.verbose "This is a verbose message." | ||
ui.info "This is an info message." | ||
ui.warning "This is a warning message." | ||
ui.error "This is an error message." | ||
ui.interactive "This is an interactive message." | ||
// By convention, 'result' calls should only happen in the method that | ||
// initially received the Invocation object. | ||
// For demonstration purposes, we call it here. | ||
ui.result "This is a result message." | ||
|
||
emit-structured cli/Cli: | ||
ui := cli.ui | ||
ui.emit-list --kind=Ui.INFO --title="A list" [1, 2, 3] | ||
ui.emit-map --kind=Ui.INFO --title="A map" { | ||
"key": "value", | ||
"key2": "value2", | ||
} | ||
ui.emit-table | ||
--kind=Ui.INFO | ||
--title="A table" | ||
--header={"name": "Name", "age": "Age"} | ||
[ | ||
{ "name": "Alice", "age": 25 }, | ||
{ "name": "Bob", "age": 30}, | ||
] | ||
|
||
main args: | ||
// Uses the application name "cli-example" which will be used | ||
// to compute the path of the config file. | ||
root-cmd := Command "cli-example" | ||
--help=""" | ||
An example application demonstrating UI usage. | ||
Run with --verbosity-level={debug, info, verbose, quiet, silent}, or | ||
--output-format={text, json} to see different output. | ||
Note that '--output-format=json' redirects some output to stderr. | ||
""" | ||
--options=[ | ||
Flag "chatty" --help="Run the chatty method" --default=false, | ||
Flag "structured" --help="Output structured data" --default=false, | ||
] | ||
--run=:: run it | ||
root-cmd.run args | ||
|
||
run invocation/Invocation: | ||
if invocation["chatty"]: | ||
some-chatty-method invocation.cli | ||
return | ||
|
||
if invocation["structured"]: | ||
emit-structured invocation.cli | ||
return | ||
|
||
ui := invocation.cli.ui | ||
ui.emit --kind=Ui.RESULT | ||
// Block that is invoked if structured data is needed. | ||
--structured=: { | ||
"result": "Computed result" | ||
} | ||
// Block that is invoked if text data is needed. | ||
--text=: "Computed result as text message." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.