szaghi / FLAP

Fortran command Line Arguments Parser for poor people
150 stars 34 forks source link

[Feature request] save bash completion #86

Open victorsndvg opened 5 years ago

victorsndvg commented 5 years ago

Hi @szaghi ,

Here I post a suggestion that I think could be interesting.

I've recently discovered save_usage_to_markdown and save_man_page procedures. It's a really smart idea to enable a binary to create its own usage help.

In addition to these procedures, I think it could be awesome to provide a new one to generate a bash-completion (or for other shell) script for further installation to let the shell to autocomplete the command line options/flags for a binary using FLAP.

https://askubuntu.com/questions/68175/how-to-create-script-with-auto-complete https://stackoverflow.com/questions/6107438/c-console-application-auto-complete-dynamic-arguments

BR

szaghi commented 5 years ago

@victorsndvg

Great Plan Scott! :-)

I'll study how to create the bash auto-complete soon, stay tuned.

szaghi commented 5 years ago

@victorsndvg

I have just uploaded a new release, 1.2.0, it has the requested feature: I have added a method to the CLI object that, once the CLI is defined, can be invoked to save a bash script file that can be sourced to add auto-completition capability to the program. It can handle also CLI with multiple CLAs groups like git that has sub-commands... I have added a specific test to demonstrate its usage, see https://github.com/szaghi/FLAP/blob/master/src/tests/flap_test_save_bash_completition.f90

This test generates the following auto-complete script

  #/usr/bin/env bash
  _completion()
    {
      cur=${COMP_WORDS[COMP_CWORD]}
      prev=${COMP_WORDS[COMP_CWORD - 1]}
      if [ "$prev" == "compile" ] ; then
        COMPREPLY=( $( compgen -W "  --compiler -c --flags -f --help -h --version -v" -- $cur ) )
      elif [ "$prev" == "clean" ] ; then
        COMPREPLY=( $( compgen -W " --clean -c --clean-all -ca --help -h --version -v" -- $cur ) )
      else
        COMPREPLY=( $( compgen -W " --help -h --version -v compile clean" -- $cur ) )
      fi
      return 0
    }
    complete -F _completion flap_test_save_bash_completition
szaghi commented 5 years ago

I have just checked and my automatic-deploy workflow is failing at some point, there is not a tagged release, however the last commit of the master is correct...

I'll fix my deploy workflow, but not today :-)

cheers

victorsndvg commented 5 years ago

Thanks @szaghi ,

I will check it (hopefully) during this week.

Great work!

victorsndvg commented 5 years ago

Hi @szaghi ,

I have done a preliminary check, and it seems that it works very well!!

Anyway, here I expose a couple of questions and propose an enhancement.

  1. I've always seen bash_completion instead of bash_completition. I don't know if the second one is also valid or it's a mistake (sorry for my ignorance), but, if not, is it possible rename it?
  2. If I don't explicitly specify a progname then, the last line of the bash completion script includes the binary name with the path. I mean bin/my_program. This makes the bash completion only work if I call my program using a matching path.
  3. In the same way that you implemented groups (checking prev), do you think it could be possible to implement completion for choices?? maybe I'm missing something ...

I will continue checking this brand new feature! I will report any news

Thanks for your effort!

szaghi commented 5 years ago

Hi @victorsndvg

thank you very much for your feedback.

  1. It is simply a typo :-)
  2. I will check, I am not sure what is happening without a program name and I am not sure if have suggested any modifications about it;
  3. I try to dig it deeper tomorrow.

cheers

szaghi commented 5 years ago

wow, I completely forgot that if the program name is not specified FLAP guess is from intrinsics... so you are suggesting to trim out the PATH and to retain only the basename, right?

@victorsndvg

szaghi commented 5 years ago

Ok, I just removed full path from progname, now it uses always the basename, great victor!

victorsndvg commented 5 years ago

Great @szaghi !!

this work is not yet published, right?

about 3), I was also thinking about it ...

I remember that choices are (in general) "valid values that can be used in the input" and no a "closed/inmutable list of acceptable values". Then, depending on the argument dimension:

What do you think? Am I missing something?

szaghi commented 5 years ago

@victorsndvg

I have just pushed a new commit (not a tagged release): it has many improvements. Regarding the original point 3 now the bash completion is really smart: if there are CLA with choices only valid value are prompted by the bash completion. Note that choices are different from varying arguments like narg=....

I think that the current behavior for the CLAs with choices is nice, now I have to think about the behavior of CLAs with nargs specified...

victorsndvg commented 5 years ago

Thanks @szaghi ,

I will check this changes and report back soon.

victorsndvg commented 5 years ago

Hi @szaghi ,

Great! choices work perfectly!

I've been studying the resulting bash-completion and I have some suggestions:

Here I write down an example taking yours as base: https://gist.github.com/victorsndvg/0620ae2304cfe7a5c2144ddbf29da470

In addition, I've discovered that groups can be duplicated in FLAP. Is this the expected behaviour? With this behavior your latest implemented FLAP test accepts the following args:

$ flap_test_save_bash_completion clean clean clean clean compile compile clean 

In my opinion, groups (like any other CLA) should not be duplicated.

I would like to check/remote duplicated CLAs also from bash completion ... but I don't know how to do it... maybe adding the following at line 23 of the gist?

      # Remove already used group from groups array
      groups=( "${groups[@]/$group}" )

this works ... but what happen if I remove a already written CLA from command line ? ... is not going to be suggested never again ...

to think

szaghi commented 5 years ago

Hi @victorsndvg

I try to reply:

Don't force a group as first argument if it's not positional

Indeed, I never realized that groups (subcommands) are not positional... I have to rethink to the bash completion implementation...

If a CLA expects a value without choices -> don't suggest nothing

Ok this should doable by the PREV argument checking,

For "special"/global CLA like --help and --version -> don't suggest nothing

It is not clear for me which are the special/global CLAs. --help and --version are automatically added to group(0) if not explicitly added, but they are (save_true) CLAs as all other CLAs.

Here I write down an example taking yours as base...

Great, this helps a lot, thank you very much!

In addition, I've discovered that groups can be duplicated in FLAP. Is this the expected behaviour?

this is a bug, I have to fix it.

I would like to check/remote duplicated CLAs also from bash completion ... but I don't know how to do it... maybe adding the following at line 23 of the gist?

I do not know, I have to think more about it.

Thanks, I'll try to fix some of the above issues soon.

victorsndvg commented 5 years ago

Thanks!