Skip to content

Allow arguments for the last flag in short flag batch - #8808

Merged
sholderbach merged 4 commits into
nushell:mainfrom
MariaSolOs:short-flag-batch-with-arg
Apr 15, 2023
Merged

sholderbach merged 4 commits into
nushell:mainfrom
MariaSolOs:short-flag-batch-with-arg

Conversation

@MariaSolOs

@MariaSolOs MariaSolOs commented Apr 8, 2023 •

Copy link
Copy Markdown
Contributor

Description

Fixes #5923

Currently nushell doesn't allow short flag batches to contain arguments, despite this being a common pattern in commands like git commit -am 'My commit message'. This PR relaxes this so that the last flag in the batch can take an argument.

User-Facing Changes

  • nu::parser::short_flag_arg_cant_take_arg has been replaced by nu::parser::only_last_flag_in_batch_can_take_arg and is displayed when a flag other then the last in a short flag batch takes an argument.

Tests + Formatting

@amtoine

amtoine commented Apr 8, 2023

Copy link
Copy Markdown
Member

👋

quite strangely, it does already work with git aliases 😮

i have

alias.cm=commit --verbose

and

git cm -am "foo"

works 😕

@MariaSolOs

Copy link
Copy Markdown
Contributor Author

quite strangely, it does already work with git aliases 😮

@amtoine Very interesting, I have some of the aliases from nu_scripts and so I didn't consider something like alias.cm=commit --verbose.

In fact, could you further explain (or point me to the right docs) what alias.X does? I had not seen that before.

@amtoine

amtoine commented Apr 8, 2023

Copy link
Copy Markdown
Member

quite strangely, it does already work with git aliases open_mouth

@amtoine Very interesting, I have some of the aliases from nu_scripts and so I didn't consider something like alias.cm=commit --verbose.

oooh i see what you're using 😋
true nushell aliases!

In fact, could you further explain (or point me to the right docs) what alias.X does? I had not seen that before.

i should have been clearer, sorry 😢
when i say i have alias.cm=commit --verbose, it means that i've configured git to run commit --verbose when i simply use the cm subcommand! (the alias.<alias>=... simply is the output of the git config --show command 😋)
these are built-in git aliases, really shortcuts if you will 😉

Note
you can see this one in my git config

@MariaSolOs

Copy link
Copy Markdown
Contributor Author

i should have been clearer, sorry 😢
when i say i have alias.cm=commit --verbose, it means that i've configured git to run commit --verbose when i simply use the cm subcommand! (the alias.=... simply is the output of the git config --show command 😋)
these are built-in git aliases, really shortcuts if you will 😉

Oh my no worries at all! I was confused because I thought you were talking about nushell's alias hehe. Thanks for clarifying!

Comment thread crates/nu-parser/src/parser.rs Outdated
orig.start + 1 + short_flag.0,
orig.start + 1 + short_flag.0 + 1,
);
let short_flag_span = Span::new(orig.start + 1 + i, orig.start + 1 + i + 1);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line previously already made the assumption that a short flag is only an ASCII character and requires just one byte. Our parsing of definitions apparently doesn't check for that causing downstream panics. I opened an issue for that (#8821)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ohh good catch!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sholderbach would you like #8821 to get fixed first, or are you okay with merging this before?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nvm, it seems like #8849 fixed the issue!

@MariaSolOs
MariaSolOs force-pushed the short-flag-batch-with-arg branch from 2393d2e to 101404f Compare April 12, 2023 01:44
@MariaSolOs
MariaSolOs marked this pull request as draft April 12, 2023 01:45
@MariaSolOs
MariaSolOs force-pushed the short-flag-batch-with-arg branch from 101404f to 48af0eb Compare April 12, 2023 01:59
@MariaSolOs
MariaSolOs force-pushed the short-flag-batch-with-arg branch from 48af0eb to 3b55612 Compare April 12, 2023 02:07
@MariaSolOs
MariaSolOs marked this pull request as ready for review April 12, 2023 02:08
@MariaSolOs

Copy link
Copy Markdown
Contributor Author

I've updated the PR to accommodate for the latest changes from #8849 :)

sholderbach added a commit that referenced this pull request Apr 14, 2023
# Description

Follow up to #8849

Work for #8821
Should unblock work on #8808

Missing is a restriction to printables or identifiers according to
[UAX31](https://www.unicode.org/reports/tr31/)

# User-Facing Changes

Non ASCII characters should be allowed in shortflags.

# Tests + Formatting

Want to add some tests for the error reporting cases
- Include the span fixes from nushell#8866
- Include the `break;` back in
- Remove unnecessary clone

@sholderbach sholderbach left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The implementation and your tests look good to me. Thank you very much!

Should be easy to update to the state of #8866

Comment thread crates/nu-parser/src/parser.rs Resolved
Comment thread crates/nu-parser/src/parser.rs Resolved
@codecov

codecov Bot commented Apr 14, 2023 •

Copy link
Copy Markdown

Codecov Report

Merging #8808 (de69f2a) into main (45d33e7) will increase coverage by 0.00%.
The diff coverage is 88.88%.

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main    #8808   +/-   ##
=======================================
  Coverage   68.51%   68.51%           
=======================================
  Files         636      636           
  Lines      101657   101657           
=======================================
+ Hits        69647    69648    +1     
+ Misses      32010    32009    -1     
Impacted Files Coverage Δ
crates/nu-protocol/src/parse_error.rs 7.79% <0.00%> (ø)
crates/nu-parser/src/parser.rs 85.35% <100.00%> (ø)

... and 5 files with indirect coverage changes

@MariaSolOs

Copy link
Copy Markdown
Contributor Author

@sholderbach For my own learning: Could you please explain what the 88.88% diff coverage refers to in the comment above? I'm reading the Codecov docs and all the other values in the summary table make sense to me, but I'm not understanding where that 88.88% is coming from...

let mut found_short_flags = vec![];
let mut unmatched_short_flags = vec![];
for (offset, short_flag) in short_flags.char_indices() {
let orig = arg_span;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no need to keep orig.

@amtoine amtoine left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good to me, that's a sweet improvement 😊

EDIT: just tested and it works like a charm ✨

@sholderbach

Copy link
Copy Markdown
Member

@sholderbach For my own learning: Could you please explain what the 88.88% diff coverage refers to in the comment above? I'm reading the Codecov docs and all the other values in the summary table make sense to me, but I'm not understanding where that 88.88% is coming from...

I think it is the fraction of the added or changed lines that are measured to be covered. But the exact numbers can be weird depending on pure definitions or test modules

@sholderbach
sholderbach merged commit 1d68c48 into nushell:main Apr 15, 2023
@MariaSolOs
MariaSolOs deleted the short-flag-batch-with-arg branch April 15, 2023 15:12
bobhy pushed a commit to bobhy/nushell that referenced this pull request Apr 15, 2023
# Description

Follow up to nushell#8849

Work for nushell#8821
Should unblock work on nushell#8808

Missing is a restriction to printables or identifiers according to
[UAX31](https://www.unicode.org/reports/tr31/)

# User-Facing Changes

Non ASCII characters should be allowed in shortflags.

# Tests + Formatting

Want to add some tests for the error reporting cases
bobhy pushed a commit to bobhy/nushell that referenced this pull request Apr 15, 2023
# Description
_Fixes #5923_

Currently `nushell` doesn't allow short flag batches to contain
arguments, despite this being a common pattern in commands like `git
commit -am 'My commit message'`. This PR relaxes this so that the last
flag in the batch can take an argument.

# User-Facing Changes
- `nu::parser::short_flag_arg_cant_take_arg` has been replaced by
`nu::parser::only_last_flag_in_batch_can_take_arg` and is displayed when
a flag other then the last in a short flag batch takes an argument.

# Tests + Formatting
- Both
[`test_parser.rs`](https://github.com/nushell/nushell/blob/48af0ebc3c9f0c7b0ef134078fe1a0e47982059e/crates/nu-parser/tests/test_parser.rs#L640-L704)
and
[`test_known_external.rs`](https://github.com/nushell/nushell/blob/48af0ebc3c9f0c7b0ef134078fe1a0e47982059e/src/tests/test_known_external.rs#L42-L61)
have been updated to test the new allowed and disallowed scenarios.

---------

Co-authored-by: sholderbach <[email protected]>
@MariaSolOs

Copy link
Copy Markdown
Contributor Author

I think it is the fraction of the added or changed lines that are measured to be covered. But the exact numbers can be weird depending on pure definitions or test modules

I still don't get it but all the diffs are green so I'm happy with that 😅

@sholderbach

Copy link
Copy Markdown
Member

I still don't get it but all the diffs are green so I'm happy with that sweat_smile

Me neither 😆

fdncred added a commit that referenced this pull request Nov 24, 2026
…19642)

## Description

`generate completions cargo` declares `-Z: string`, and with that extern
loaded, `cargo -Zscript foo.rs` failed with
`only_last_flag_in_batch_can_take_arg`. `^cargo -Zscript` and `cargo -Z
script` both worked. `make -j8` and `git commit -amf` failed the same
way under their externs. `git commit -amf` is the example in #5923,
which #8808 closed by letting the last flag in a batch take the next
word. The attached form never worked.

A known external now reads short flags the way `getopt` does. When a
flag that takes a value comes before the end of the word, the rest of
the word is its value, and the word goes to the program as written.
Nushell still checks that value against the flag's type, so `-jabc` with
`-j: int` fails like `-j abc` does. `extern --strict` accepts the word
when every flag in it is declared. Nushell commands and `def --wrapped`
keep the old rule.

```nushell
> cargo -Zscript fp_asm.rs        # before
Error: nu::parser::only_last_flag_in_batch_can_take_arg

  × Only the last flag in a short flag batch can take an argument.
   ╭─[source:1:37]
 1 │ source cargo_completions.nu; cargo -Zscript fp_asm.rs | complete | reject stdout
   ·                                     ┬
   ·                                     ╰── only the last flag can take args

> cargo -Zscript fp_asm.rs        # after, on stable cargo
error: the `-Z` flag is only accepted on the nightly channel of Cargo, but this is the `stable` channel
```

## User-facing changes (Release notes)

Commands declared with `extern` accept a short flag with its value
attached, such as `cargo -Zscript`, `make -j8` or `git commit -amf`, and
pass it to the program as written.

## Additional notes

closes #19639

- Values that start with `$` or `(` skip the type check. Inside the
word, `-Z$x` stays literal and `-Z(1 + 2)` becomes `-Z3`, so checking
`$x` or `(1 + 2)` alone would test something the program never gets.
- The last-flag check also had a bug. It compared a byte offset with a
character count, so in `-éjm 5` the `-j` took a value without being
last. It now compares spans.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support short flag batches that take args for external completions

3 participants