Skip to content

Remove redundant token functions #8008

Closed

Description

@kidonng

Currently there are three functions for determining commandline token location:

The first two are doing the same thing, and the third can be achieved using the first two functions.

I propose to keep the first second function since it looks more sophisticated than the second first and has a better name. This may break a couple of userland functions, but that's what happens if you rely on some fish internals 😄

Activity

  1. zanchey commented on May 16, 2021

    @zanchey
    Member

    Sounds good.

  2. added this to the fish-future milestone on May 16, 2021
  3. krobelus commented on May 16, 2021

    @krobelus
    Contributor

    __fish_is_nth_token sounds clearer IMO.

    We can also un-dunder them (removing the __ prefix)

  4. kidonng commented on May 17, 2021

    @kidonng
    ContributorAuthor

    __fish_is_nth_token sounds clearer IMO.

    Ah sorry, I was meant to say keeping that one. Maybe I messed up the order.

    We can also un-dunder them (removing the __ prefix)

    That's certainly better! 👍 I can see they are used a lot in completions.


    Also worth noting, on closer looking, __fish_is_nth_token excludes arguments (or more precisely, token starting with dash -), which looks too magic to me. __fish_is_token_n is simple and effective, so I suggest removing the former and rename the latter to fish_is_nth_token.

  5. faho commented on May 17, 2021

    @faho
    Member

    Also worth noting, on closer looking, __fish_is_nth_token excludes arguments (or more precisely, token starting with dash -), which looks too magic to me. __fish_is_token_n is simple and effective, so I suggest removing the former and rename the latter to fish_is_nth_token.

    Please don't. It's not "magic", it's pragmatic. This is used in completion scripts, and for those options are special. Removing them is correct 99% of the time (tho it won't deal with option arguments, but that requires knowing the options beforehand - i.e. using argparse).

  6. kidonng commented on May 17, 2021

    @kidonng
    ContributorAuthor

    You're right, I just realized options should be excluded most of time since they don't have a fixed location and count.

  7. butterthebuddha commented on Jun 30, 2021

    @butterthebuddha

    I'd like to pick this up. Does the "good first issue" label mean "good issue for first-time fish contributors" or "good issue for first-time open-source contributors"?

  8. zanchey commented on Jun 30, 2021

    @zanchey
    Member

    Hopefully both!

  9. butterthebuddha commented on Jun 30, 2021

    @butterthebuddha

    A lot of completion scripts are passing __fish_is_first_token to complete using the -n flag. In light of that, can I propose we keep __first_is_first_token and instead edit its implementation to use fish_is_nth_token?

  10. zanchey commented on Jun 30, 2021

    @zanchey
    Member

    It would be fine to do that, even as a first step to consolidate the implementation.

  11. kidonng commented on Jul 20, 2021

    @kidonng
    ContributorAuthor

    There's this __fish_is_first_arg that does similar things, though I would argue it is useful (as it doesn't ignore options) and should be kept.

    Also I wonder if we can implement __fish_not_contain_opt and __fish_no_arguments with __fish_seen_argument, in the same fashion.

    And there's even __fish_first_token...

  12. kidonng commented on Jul 25, 2021

    @kidonng
    ContributorAuthor

    #8096 has been merged, so I guess this can be closed.

  13. mqudsi commented on Feb 8, 2022

    @mqudsi
    Contributor

    One nice thing about internal functions is that their name doesn't matter. Case in point: it always bothered me that this was called "...token" when it is almost entirely the opposite of that. A token is the atomic, this function skips over the command and any flags or switches. Can we consider renaming it before 3.4.0 ships?

    For the following example:

    sudo foo --help add -f foo.fish -- foo2.fish
    

    Technically, sudo and foo are both tokens that are ignored as commands, --help is a token that's classified as an option, -f is a token that's classified as a flag, add is a token classified as a subcommand, foo.fish is a token classified as, I don't know, argument? -- is a token that's classified as "unix commandline wizardry" and foo2.fish is another argument/payload. We only consider add, foo.fish and foo2.fish here.

    What's a better name? Is "argument" generic enough? A case could be made for add to be classified as an argument further classified as a subcommand. But a case could be made that anything after sudo (or more generously, after foo) is an argument (see argc and argv). Would you consider flags and switches to be arguments? Is token the best we can do after all?

  14. reopened this on Feb 8, 2022
  15. krobelus commented on Feb 9, 2022

    @krobelus
    Contributor

    Thanks for bringing this up.

    I'd think "argument" is probably the best term here, but as you pointed out, it can be misleading.
    I originally suggested un-dundering this but in hindsight that seems wrong.
    I think this leaky abstraction is worse than no abstraction.
    The commandline builtin already offers a consistent API; users should learn that instead of an incomplete set of functions.
    This way we keep fish_<Tab> more useful to the average user.

    I skimmed through all functions containing "tok", "arg" or "opt", and this
    is the list of command-agnostic functions:

    share/functions/__fish_any_arg_in.fish
    share/functions/__fish_contains_opt.fish
    share/functions/__fish_is_first_arg.fish
    share/functions/__fish_no_arguments.fish
    share/functions/__fish_not_contain_opt.fish
    share/functions/__fish_number_of_cmd_args_wo_opts.fish
    share/functions/__fish_prev_arg_in.fish
    share/functions/__fish_print_cmd_args.fish
    share/functions/__fish_print_cmd_args_without_options.fish
    share/functions/__fish_print_cmd_args_without_options.fish
    share/functions/__fish_seen_argument.fish
    share/functions/__fish_first_token.fish
    share/functions/__fish_is_first_token.fish
    share/functions/__fish_is_nth_token.fish
    share/functions/fish_is_nth_token.fish
    share/functions/__fish_is_token_n.fish
    share/functions/__fish_list_current_token.fish
    share/functions/fish_nth_token.fish
    

    fish_nth_token and fish_is_nth_token are clearly the odd ones,
    so I'd go back to making them internal functions only.

    There are several more inconsistencies; I think it's economical to clean
    them up in one pass.
    Once we revisit this we should probably consolidate a bunch of features in single
    command (to avoid namespace pollution). Something like commandline status.

  16. kidonng commented on Feb 9, 2022

    @kidonng
    ContributorAuthor

    Once we revisit this we should probably consolidate a bunch of features in single
    command (to avoid namespace pollution). Something like commandline status.

    That would be very nice. I hope there would be a more ergonomic way to write some checks like test (count (commandline -opc)) -lt 2.

  17. locked as resolved and limited conversation to collaborators on Jun 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions