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

How to add command abbreviation or command alias #59

Closed
diam opened this issue Apr 26, 2018 · 4 comments · Fixed by #91
Closed

How to add command abbreviation or command alias #59

diam opened this issue Apr 26, 2018 · 4 comments · Fixed by #91

Comments

@diam
Copy link

diam commented Apr 26, 2018

Bonjour,

I'd like to be able to enter one of the shortest unambiguous abbreviation in place of a command name. As it doesn't seam possible, is it possible to add some alias to command names?
I've tried the following code but it is rejected:

    @add_arg_table s begin
        "generate", "gen", "g"
            action = "command"
            help = "..."
        "eval", "ev", "e"
            action = "command"
            help = "..."
    end

But this raise the following error :

    ERROR: LoadError: only options can have multiple names

Regard,
-- Maurice

@carlobaldassi
Copy link
Owner

You are right that this may make sense. However, it's not straightforward to implement and adds another layer of complexity. Couldn't you just use the option form for commands instead, e.g. --generate, --eval etc.?

@diam
Copy link
Author

diam commented Apr 27, 2018

I could use some --generate or --eval option. In fact I'd rather use something like --action generate or --action eval. Then I could filter the action value and process it as unambiguous abbreviation of some set of strings (see the abbrev function below).
However the command type argument has the advantage to allow different options set for a particular command.

I don't know the detail implementation of ArgParse, but I imagine that it would be possible to considere a command as a first positional argument, then try to match it as an abbrev of an existing command (else howl!).

Here is a function I've stollen from a Ruby Library, then converted to Julia.
-- Maurice

#!/usr/bin/env julia


# Inspiré du gem abbrev de Ruby
#   http://ruby-doc.org/stdlib-2.5.0/libdoc/abbrev/rdoc/Abbrev.html
#   https://github.com/ruby/ruby/blob/trunk/lib/abbrev.rb
#
# Given a set of strings, calculate the set of unambiguous abbreviations for
# those strings, and return a hash where the keys are all the possible
# abbreviations and the values are the full strings.
#
# Thus, given +words+ is "car" and "cone", the keys pointing to "car" would
# be "ca" and "car", while those pointing to "cone" would be "co", "con", and
# "cone".
#
#   abbrev(["car", "cone"])
#   #=> {"ca"=>"car", "con"=>"cone", "co"=>"cone", "car"=>"car", "cone"=>"cone"}
#
# The optional +pattern+ parameter is a pattern or a string. Only input
# strings that match the pattern or start with the string are included in the
# output hash.
#
#   Abbrev.abbrev(%w{car box cone crab}, /b/)
#   #=> {"box"=>"box", "bo"=>"box", "b"=>"box", "crab" => "crab"}
#
#   Abbrev.abbrev(%w{car box cone}, 'ca')
#   #=> {"car"=>"car", "ca"=>"car"}
#
function abbrev(words::Array{String,1}, pat=r".*")
    table = Dict{String, Symbol}()
    seen = Dict{String, Int}()
    for word in words
        length(word)==0 && next
        for len in length(word):-1:1
            abbrev = word[1:len]
            if !ismatch(pat, abbrev)
                continue
            end
            seen[abbrev] = 1 + get(seen, abbrev, 0)
            if seen[abbrev] == 1
                table[abbrev] = word
            elseif seen[abbrev] == 2
                delete!(table, abbrev)
            else
                break
            end
        end
    end
    for word in words
        if !ismatch(pat, word)
            continue
        end
        table[word] = word
    end
    table
end

@show ARGS
@show abbrev(ARGS)
@show abbrev(["car", "cone"])

@carlobaldassi
Copy link
Owner

You can use flag options as commands. For example, you can have an option --generate and set it as action = :command. This will behave as you wanted, i.e. it has its own sub-session with command-specific options and arguments, allows abbreviations/aliases and even short form (-g), etc. Example.

@diam
Copy link
Author

diam commented May 29, 2018

Thank you very much Carlo. I missed the fact that dashed-options can be used as command names. So one can easily abbreviate them.

But commands starting with dashes are not natural as Command Like Interfaces. So I am probably going to keep using standard command names (but with short names and without aliases).

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 a pull request may close this issue.

2 participants