Skip to content

feat: add --coerce for def - #18406

Closed
Skyrocket1653 wants to merge 4 commits into
nushell:mainfrom
Skyrocket1653:coerce
Closed

Skyrocket1653 wants to merge 4 commits into
nushell:mainfrom
Skyrocket1653:coerce

Conversation

@Skyrocket1653

@Skyrocket1653 Skyrocket1653 commented Jun 13, 2026 •

Copy link
Copy Markdown
Contributor

Description

Adds a --coerce which behavior is like --wrapped but respects --.

User-facing changes (Release notes)

Now this is possible:

def --coerce example [--my-flag: string ...rest] { $rest | each { {type: ($in | describe) value: $in} } | print; print $"--my-flag='($my_flag)'" }
example --my-flag="hi" -- true false 001 --my-flag="goodbye"
┌───┬────────┬───────────────────┐
│ # │  type  │       value       │
├───┼────────┼───────────────────┤
│ 0 │ string │ true              │
├───┼────────┼───────────────────┤
│ 1 │ string │ false             │
├───┼────────┼───────────────────┤
│ 2 │ string │ 001               │
├───┼────────┼───────────────────┤
│ 3 │ string │ --my-flag=goodbye │
└───┴────────┴───────────────────┘
--my-flag='hi'

Additional notes

Closes #18399

@github-actions github-actions Bot added the A:parser Issues related to parsing label Jun 13, 2026
@fdncred

fdncred commented Jun 13, 2026

Copy link
Copy Markdown
Contributor

If we move forward with this (not sure we will) I probably wouldn't allow --coerce and -c. None of the other flags have shorts.

Also, I think │ 3 │ string │ --my-flag=goodbye │ should be │ 3 │ string │ --my-flag="goodbye" │

@Skyrocket1653

Copy link
Copy Markdown
Contributor Author
def --wrapped example [...rest] { print ...$rest }
example --my-flag="hi"
--my-flag=hi

--coerce is basically --wrapped so if --wrapped also does it --coerce does too.

@fdncred

fdncred commented Jun 13, 2026

Copy link
Copy Markdown
Contributor

I have another idea

@Skyrocket1653

Copy link
Copy Markdown
Contributor Author

What is your idea?

@fdncred

fdncred commented Jun 13, 2026 •

Copy link
Copy Markdown
Contributor

There's a bug in the end-of-options code I think. I have this working right now in my PR. Does this satisfy your use case?

❯ def --wrapped example [--my-flag: string ...rest] {
  let rest = $rest | skip 1  # skip the '--' delimiter
  $rest | each { {type: ($in | describe) value: $in} } | print
  print $"--my-flag='($my_flag)'"
}
❯ example --my-flag="hi" -- true false 001 --my-flag="goodbye"
╭─#─┬──type──┬───────value───────╮
│ 0 │ string │ true              │
│ 1 │ string │ false             │
│ 2 │ string │ 001               │
│ 3 │ string │ --my-flag=goodbye │
╰───┴────────┴───────────────────╯
--my-flag='hi'

@Skyrocket1653

Copy link
Copy Markdown
Contributor Author

That does not work on my side.

def --wrapped example [--my-flag: string ...rest] {
  let rest = $rest | skip 1  # skip the '--' delimiter
  $rest | each { {type: ($in | describe) value: $in} } | print
  print $"--my-flag='($my_flag)'"
}
example --my-flag="hi" -- true false 001 --my-flag="goodbye"
┌───┬────────┬───────┐
│ # │  type  │ value │
├───┼────────┼───────┤
│ 0 │ string │ true  │
├───┼────────┼───────┤
│ 1 │ string │ false │
├───┼────────┼───────┤
│ 2 │ string │ 001   │
└───┴────────┴───────┘
--my-flag='goodbye'

As you can see -- is not doing anything so my-flag="goodbye" overrides the first one.

@Skyrocket1653

Copy link
Copy Markdown
Contributor Author

--wrapped makes -- as a rest value instead of being an separator (someone in the issue explained that they need this behaviour for their usecase) thus it is not possible to pass flags with the same name defined in the def as a rest value.

My flag --coerce simply makes the first -- be an actual separator so you can pass flags with the same name defined in def as a rest value as long as -- is used.

@Skyrocket1653

Copy link
Copy Markdown
Contributor Author

I've also removed -c as you said it wasn't allowed.

@Skyrocket1653

Copy link
Copy Markdown
Contributor Author

Oh wait I didn't see the pr you just made, I'm going to try it first.

@fdncred

fdncred commented Jun 13, 2026

Copy link
Copy Markdown
Contributor

closed in favor of #18408

@fdncred fdncred closed this Jun 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A:parser Issues related to parsing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Coerce rest into globs

2 participants