Skip to content

Commit

Permalink
Correct blunder regarding --query
Browse files Browse the repository at this point in the history
Note that clap doesn't support (--foo [--bar] | --quux) groups very
cleanly; it was a bit of a hack to get this to work, with the result
being the error text being a bit off when an illegal combination is
attempted. I've attempted to compensate for this by making the long help
text quite explicit.

Also updated clap, which contains the fix for clap-rs/clap#5022
  • Loading branch information
Xophmeister committed Aug 17, 2023
1 parent a34353c commit 31efb52
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 64 deletions.
15 changes: 8 additions & 7 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ After (use IO redirection):
```
topiary [--skip-idempotence] \
[--tolerate-parsing-errors] \
(--langauge LANGUAGE | --query QUERY) \
(--langauge LANGUAGE [--query QUERY]) \
< INPUT_FILE \
> OUTPUT_FILE
```
Expand All @@ -106,7 +106,7 @@ Before:
```
topariy [--skip-idempotence] \
[--tolerate-parsing-errors] \
(--langauge LANGUAGE | --query QUERY) \
(--langauge LANGUAGE [--query QUERY]) \
(--input-files - | < INPUT_FILE) \
[--output-file -]
```
Expand All @@ -115,7 +115,7 @@ After (use IO redirection):
```
topiary [--skip-idempotence] \
[--tolerate-parsing-errors] \
(--langauge LANGUAGE | --query QUERY) \
(--langauge LANGUAGE [--query QUERY]) \
< INPUT_FILE
```

Expand All @@ -142,15 +142,15 @@ topiary vis [--format FORMAT] \
Before:
```
topiary --visualise[=FORMAT] \
(--langauge LANGUAGE | --query QUERY) \
(--langauge LANGUAGE [--query QUERY]) \
< INPUT_FILE \
[--output-file OUTPUT_FILE | > OUTPUT_FILE]
```

After (use IO redirection):
```
topiary vis [--format FORMAT] \
(--langauge LANGUAGE | --query QUERY) \
(--langauge LANGUAGE [--query QUERY]) \
< INPUT_FILE \
[> OUTPUT_FILE]
```
Expand Down Expand Up @@ -184,12 +184,13 @@ topiary --configuration CONFIG_FILE \

###### Examining Computed Configuration

Before (to standard error, then proceeding with other functions):
Before (to standard error, as debug output, then proceeding with other
functions):
```
topiary --output-configuration ...
```

After (to standard output, as a dedicated function):
After (to standard output, in TOML format, as a dedicated function):
```
topiary cfg
```
Expand Down
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

64 changes: 35 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,12 +189,12 @@ Options:
[default: merge]
Possible values:
- merge: When multiple sources of configuration are available, matching items are updated from
the higher priority source, with collections merged as the union of sets
- revise: When multiple sources of configuration are available, matching items (including
collections) are superseded from the higher priority source
- override: When multiple sources of configuration are available, the highest priority source is
taken. All values from lower priority sources are discarded
- merge: When multiple sources of configuration are available, matching items are
updated from the higher priority source, with collections merged as the union of sets
- revise: When multiple sources of configuration are available, matching items
(including collections) are superseded from the higher priority source
- override: When multiple sources of configuration are available, the highest priority
source is taken. All values from lower priority sources are discarded
-h, --help
Print help (see a summary with '-h')
Expand All @@ -211,12 +211,15 @@ Options:
```
Format inputs
Usage: topiary fmt [OPTIONS] <--language <LANGUAGE>|--query <QUERY>|FILES>
Usage: topiary fmt [OPTIONS] <--language <LANGUAGE>|FILES>
Arguments:
[FILES]...
Input files and directories (omit to read from stdin)
Language detection and query selection is automatic, mapped from file extensions defined
in the Topiary configuration.
Options:
-t, --tolerate-parsing-errors
Consume as much as possible in the presence of parsing errors
Expand All @@ -230,7 +233,7 @@ Options:
[possible values: json, nickel, ocaml, ocaml-interface, ocamllex, toml]
-q, --query <QUERY>
Topiary query file (for formatting stdin)
Topiary query file override (when formatting stdin)
-C, --configuration <CONFIGURATION>
Configuration file
Expand All @@ -244,12 +247,12 @@ Options:
[default: merge]
Possible values:
- merge: When multiple sources of configuration are available, matching items are updated from
the higher priority source, with collections merged as the union of sets
- revise: When multiple sources of configuration are available, matching items (including
collections) are superseded from the higher priority source
- override: When multiple sources of configuration are available, the highest priority source is
taken. All values from lower priority sources are discarded
- merge: When multiple sources of configuration are available, matching items are
updated from the higher priority source, with collections merged as the union of sets
- revise: When multiple sources of configuration are available, matching items
(including collections) are superseded from the higher priority source
- override: When multiple sources of configuration are available, the highest priority
source is taken. All values from lower priority sources are discarded
-h, --help
Print help (see a summary with '-h')
Expand All @@ -267,12 +270,15 @@ either `--language` or `--query` arguments, omitting any input files.
```
Visualise the input's Tree-sitter parse tree
Usage: topiary vis [OPTIONS] <--language <LANGUAGE>|--query <QUERY>|FILE>
Usage: topiary vis [OPTIONS] <--language <LANGUAGE>|FILE>
Arguments:
[FILE]
Input file (omit to read from stdin)
Language detection and query selection is automatic, mapped from file extensions defined
in the Topiary configuration.
Options:
-f, --format <FORMAT>
Visualisation format
Expand All @@ -289,7 +295,7 @@ Options:
[possible values: json, nickel, ocaml, ocaml-interface, ocamllex, toml]
-q, --query <QUERY>
Topiary query file (for formatting stdin)
Topiary query file override (when formatting stdin)
-C, --configuration <CONFIGURATION>
Configuration file
Expand All @@ -303,12 +309,12 @@ Options:
[default: merge]
Possible values:
- merge: When multiple sources of configuration are available, matching items are updated from
the higher priority source, with collections merged as the union of sets
- revise: When multiple sources of configuration are available, matching items (including
collections) are superseded from the higher priority source
- override: When multiple sources of configuration are available, the highest priority source is
taken. All values from lower priority sources are discarded
- merge: When multiple sources of configuration are available, matching items are
updated from the higher priority source, with collections merged as the union of sets
- revise: When multiple sources of configuration are available, matching items
(including collections) are superseded from the higher priority source
- override: When multiple sources of configuration are available, the highest priority
source is taken. All values from lower priority sources are discarded
-h, --help
Print help (see a summary with '-h')
Expand Down Expand Up @@ -342,12 +348,12 @@ Options:
[default: merge]
Possible values:
- merge: When multiple sources of configuration are available, matching items are updated from
the higher priority source, with collections merged as the union of sets
- revise: When multiple sources of configuration are available, matching items (including
collections) are superseded from the higher priority source
- override: When multiple sources of configuration are available, the highest priority source is
taken. All values from lower priority sources are discarded
- merge: When multiple sources of configuration are available, matching items are
updated from the higher priority source, with collections merged as the union of sets
- revise: When multiple sources of configuration are available, matching items
(including collections) are superseded from the higher priority source
- override: When multiple sources of configuration are available, the highest priority
source is taken. All values from lower priority sources are discarded
-h, --help
Print help (see a summary with '-h')
Expand Down Expand Up @@ -405,7 +411,7 @@ sources where Topiary checks for such a file.

### Configuration Sources

At buildtime the [languages.toml](./languages.toml) in the root of
At build time the [languages.toml](./languages.toml) in the root of
this repository is embedded into Topiary. This file is parsed at
runtime. The purpose of this `languages.toml` file is to provide sane
defaults for users of Topiary (both the library and the binary).
Expand Down
1 change: 0 additions & 1 deletion topiary-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ path = "src/main.rs"
[dependencies]
# For now we just load the tree-sitter language parsers statically.
# Eventually we will want to dynamically load them, like Helix does.
# NOTE clap/wrap_help isn't perfect (see clap-rs/clap#5022)
clap = { workspace = true, features = ["derive", "env", "wrap_help"] }
directories = { workspace = true }
env_logger = { workspace = true }
Expand Down
53 changes: 30 additions & 23 deletions topiary-cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,52 +51,59 @@ pub struct GlobalArgs {
pub configuration_collation: Option<configuration::CollationMode>,
}

#[derive(Args, Debug)]
pub struct Stdin {
/// Topiary supported language (for formatting stdin)
#[arg(short, long)]
pub language: SupportedLanguage,

/// Topiary query file override (when formatting stdin)
#[arg(short, long, requires = "language")]
pub query: Option<PathBuf>,
}

// Subtype for exactly one input:
// * FILE => Read input from disk, visualisation output to stdout
// * --language | --query => Read input from stdin, visualisation output to stdout
// * FILE => Read input from disk, visualisation output to stdout
// * --language => Read input from stdin, visualisation output to stdout
#[derive(Args, Debug)]
#[command(
// Require exactly one of --language, --query, or FILES...
// Require exactly one of --language, or FILES...
group = ArgGroup::new("source")
.multiple(false)
.required(true)
.args(&["language", "query", "file"])
.args(&["language", "file"])
)]
pub struct ExactlyOneInput {
/// Topiary supported language (for formatting stdin)
#[arg(short, long)]
language: Option<SupportedLanguage>,

/// Topiary query file (for formatting stdin)
#[arg(short, long)]
query: Option<PathBuf>,
#[command(flatten)]
pub stdin: Option<Stdin>,

/// Input file (omit to read from stdin)
file: Option<PathBuf>,
///
/// Language detection and query selection is automatic, mapped from file extensions defined in
/// the Topiary configuration.
pub file: Option<PathBuf>,
}

// Subtype for at least one input
// * FILES... => Read input(s) from disk, format in place
// * --language | --query => Read input from stdin, output to stdout
// * FILES... => Read input(s) from disk, format in place
// * --language => Read input from stdin, output to stdout
#[derive(Args, Debug)]
#[command(
// Require exactly one of --language, --query, or FILES...
group = ArgGroup::new("source")
.multiple(false)
.required(true)
.args(&["language", "query", "files"])
.args(&["language", "files"])
)]
pub struct AtLeastOneInput {
/// Topiary supported language (for formatting stdin)
#[arg(short, long)]
language: Option<SupportedLanguage>,

/// Topiary query file (for formatting stdin)
#[arg(short, long)]
query: Option<PathBuf>,
#[command(flatten)]
pub stdin: Option<Stdin>,

/// Input files and directories (omit to read from stdin)
files: Vec<PathBuf>,
///
/// Language detection and query selection is automatic, mapped from file extensions defined in
/// the Topiary configuration.
pub files: Vec<PathBuf>,
}

#[derive(Debug, Subcommand)]
Expand Down

0 comments on commit 31efb52

Please sign in to comment.