Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

function takes invalid variable names for --argument-names #6147

Closed
ammgws opened this issue Sep 27, 2019 · 3 comments
Closed

function takes invalid variable names for --argument-names #6147

ammgws opened this issue Sep 27, 2019 · 3 comments
Labels
bug Something that's not working as intended
Milestone

Comments

@ammgws
Copy link
Contributor

ammgws commented Sep 27, 2019

When running the following function,

function testscope
  set DEBUG 1

  function test1 --no-scope-shadowing --argument-names a b
    echo (status function)" debug=$DEBUG (args: $argv)"
  end

  function test2 --argument-names a b --no-scope-shadowing
    echo (status function)" debug=$DEBUG (args: $argv)"
  end

  function test3 --argument-names "a b" --no-scope-shadowing
    echo (status function)" debug=$DEBUG (args: $argv)"
  end

  set names "a b"
  function test4 --argument-names $names --no-scope-shadowing
    echo (status function)" debug=$DEBUG (args: $argv)"
  end

  test1 "foo" "bar"
  test2 "foo" "bar"
  test3 "foo" "bar"
  test4 "foo" "bar"
end

I get this output:

test1 debug=1 (args: foo bar)
test2 debug= (args: foo bar)
test3 debug=1 (args: foo bar)
test4 debug=1 (args: foo bar)

I take it this happens because REQUIRE_ORDER is used when parsing the flags here?

REQUIRE_ORDER means don't recognize them as options; stop option processing when the first non-option is seen. This is what Unix does. This mode of operation is selected by using `+' as the first character of the list of option characters.

Should this be documented in the docs for function, or does it go without saying?

@faho
Copy link
Member

faho commented Oct 2, 2019

Well, no. This isn't using REQUIRE_ORDER specifically.

It's understanding --no-scope-shadowing as a name for a variable.

Which is awful because $--no-scope-shadowing can't be expanded as a variable (because - isn't actually valid in variable names!)

@faho
Copy link
Member

faho commented Oct 2, 2019

Test-case:

function testfun --argument-names a b c --no-scope-shadowing
   set -l
end

Presumably this should error out.

@zanchey zanchey added the bug Something that's not working as intended label Oct 3, 2019
@zanchey zanchey added this to the fish-future milestone Oct 3, 2019
@faho faho changed the title Document the use of "REQUIRE_ORDER" in function function takes invalid variable names for --argument-names Oct 7, 2019
@faho faho closed this as completed in a7913c3 Oct 7, 2019
@6A61736F6E206E61646572
Copy link
Contributor

6A61736F6E206E61646572 commented Oct 7, 2019

Cheers.

Just to check: REQUIRE_ORDER is needed so that NAME in function NAME [OPTIONS] keeps its position before OPTIONS, otherwise getopts will move to the end where it could become an arg to --argument-names if that was the last defined option?

// This command is atypical in using the "+" (REQUIRE_ORDER) option for flag parsing.
// This is needed due to the semantics of the -a/--argument-names flag.

This is what is confusing me, from here, assuming fish's getopts implementation is the same:

If require_order is enabled, options processing terminates when the first non-option is encountered.
--foo arg1 --bar arg2 arg3
is equivalent to
--foo -- arg1 --bar arg2 arg3

So with testfun --argument-names a b c --no-scope-shadowing, it would turn into
testfun --argument-names -- a b c --no-scope-shadowing where ARGV is "a", "b", "c", "--no-scope-shadowing`, but that's clearly not the case.. :/

@zanchey zanchey modified the milestones: fish-future, fish 3.1.0 Oct 20, 2019
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 16, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something that's not working as intended
Projects
None yet
Development

No branches or pull requests

4 participants