Skip to content

Commit

Permalink
can hide short flags via config file
Browse files Browse the repository at this point in the history
  • Loading branch information
drnic committed Nov 19, 2008
1 parent f95ae59 commit 4e3e35d
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 19 deletions.
15 changes: 15 additions & 0 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,26 @@ The following syntax options for the 'run' command are functionally equivalent:

=== Application name aliases

IN CURRENT DEVELOPMENT

I don't ever type 'script/generate', rather I have an alias 'gen' for it.

TabTab supports user-defined aliases (you might have an alias 'g' for 'script/generate') via its
~/.tabtab.yml configuration file. See Aliases section below.

=== Hide small flags

IN CURRENT DEVELOPMENT

Many applications have command-line flag arguments, such as --port (long) or -p (short).
The short form is useful if you are typing them yourself - they are two characters long.
If you are using auto-completions, it may not be meaningful nor useful to see the
short form flags.

You can disable short-form versions of flags via the ~/.tabtab.yml config file:

shortflags: disable

== INSTALL:

* sudo gem install tabtab
Expand Down
19 changes: 19 additions & 0 deletions features/hide_short_flags.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Feature: Do not display short flags (-p) within lists of completions options
In order to reduce cost of scanning through lists of completion options
As a tabtab user
I want to be able to disable short flags and never see them

Scenario: Explicitly disable short flags via the config file for definitions
Given a .tabtab.yml config file
And a file 'my_definitions.rb' containing completion definitions
And disable short flags
When run local executable 'tabtab' with arguments '--file my_definitions.rb test_app "" test_app'
Then I should not see any short form flags

Scenario: Explicitly disable short flags via the config file for externals
Given a .tabtab.yml config file
And disable short flags
And env variable $PATH includes fixture executables folder
When run local executable 'tabtab' with arguments '--external test_app "" test_app'
Then I should not see any short form flags

13 changes: 8 additions & 5 deletions features/steps/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ def in_home_folder(&block)
end

Given %r{^a safe folder} do
FileUtils.rm_rf @tmp_root = File.dirname(__FILE__) + "/../../tmp"
FileUtils.mkdir_p @tmp_root
FileUtils.mkdir_p @home_path = File.expand_path(File.join(@tmp_root, "home"))
@lib_path = File.expand_path(File.dirname(__FILE__) + '/../../lib')
Given "env variable $HOME set to '#{@home_path}'"
unless @have_created_safe_folder
FileUtils.rm_rf @tmp_root = File.dirname(__FILE__) + "/../../tmp"
FileUtils.mkdir_p @tmp_root
FileUtils.mkdir_p @home_path = File.expand_path(File.join(@tmp_root, "home"))
@lib_path = File.expand_path(File.dirname(__FILE__) + '/../../lib')
Given "env variable $HOME set to '#{@home_path}'"
@have_created_safe_folder = true
end
end

Given %r{^this project is active project folder} do
Expand Down
6 changes: 6 additions & 0 deletions features/steps/completions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,9 @@
expected_output = %w[--extra --help].join("\n")
expected_output.should == actual_output.strip
end

Then /^I should not see any short form flags$/ do
actual_output = File.read(File.join(@tmp_root, "executable.out"))
expected_output = %w[--extra --help].join("\n")
expected_output.should == actual_output.strip
end
10 changes: 10 additions & 0 deletions features/steps/configuration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Given /^disable short flags$/ do
in_home_folder do
config = YAML.load(File.read('.tabtab.yml'))
config['shortflags'] = 'disable'
File.open('.tabtab.yml', 'w') do |f|
f << config.to_yaml
end
end
end

15 changes: 12 additions & 3 deletions lib/tabtab/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class CLI

attr_reader :stdout
attr_reader :app_type, :full_line
attr_reader :global_config

def self.execute(stdout, arguments=[])
self.new.execute(stdout, arguments)
Expand All @@ -17,6 +18,7 @@ def execute(stdout, arguments=[])
# @full_line = ENV['COMP_LINE']
# @full_line_argv = Shellwords.shellwords(@full_line)
return "" unless @app_type = arguments.shift
load_global_config
case @app_type.gsub(/^-*/, '').to_sym
when :external
process_external *arguments
Expand Down Expand Up @@ -46,23 +48,30 @@ def externals
end

def external_options(app, options_flag)
TabTab::Completions::External.new(app, options_flag)
TabTab::Completions::External.new(app, options_flag, global_config)
end

#
# Support for RubyGem-based completion definitions (found in any gem path)
# --gem gem_name
#
def process_gem arguments
stdout.puts TabTab::Completions::Gem.new(*arguments).extract.join("\n")
gem_name, app_name, current_token, previous_token = arguments
stdout.puts TabTab::Completions::Gem.new(gem_name, app_name, current_token, previous_token, global_config).extract.join("\n")
end

#
# Support for file-based completion definitions (found in target file)
# --file /path/to/definition.rb
#
def process_file arguments
stdout.puts TabTab::Completions::File.new(*arguments).extract.join("\n")
file_path, app_name, current_token, previous_token = arguments
stdout.puts TabTab::Completions::File.new(file_path, app_name, current_token, previous_token, global_config).extract.join("\n")
end

def load_global_config
@global_config ||= {}
@global_config[:shortflags] = config["shortflags"] || "enable"
end

def usage
Expand Down
13 changes: 10 additions & 3 deletions lib/tabtab/completions/external.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
class TabTab::Completions::External
def initialize(app_name, options_flag = '-h')
attr_reader :global_config

def initialize(app_name, options_flag = '-h', global_config = {})
@app_name = app_name
@options_flag = options_flag
@global_config = global_config
end

def options_str
Expand All @@ -17,8 +20,8 @@ def extract(_options_str = nil)
all_options = lines_containing_options.inject([]) do |list, line|
list + line.scan(/(?:^\s+|,\s)(-[\w-]+)/).flatten
end
long_options = all_options.grep(/^--/).sort
short_options = (all_options - long_options).sort
long_options = all_options.grep(/^--/).sort
short_options = hide_short_flags? ? [] : (all_options - long_options).sort
long_options + short_options
end
end
Expand All @@ -29,4 +32,8 @@ def extract(_options_str = nil)
def starts_with(prefix)
extract.grep(/^#{prefix}/)
end

def hide_short_flags?
global_config[:shortflags] == 'disable'
end
end
7 changes: 4 additions & 3 deletions lib/tabtab/completions/file.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
class TabTab::Completions::File
attr_reader :file_path, :app_name, :current_token, :previous_token
attr_reader :file_path, :app_name, :current_token, :previous_token, :global_config

def initialize(file_path, app_name, current_token, previous_token)
def initialize(file_path, app_name, current_token, previous_token, global_config = {})
@file_path = file_path
@app_name = app_name
@current_token = current_token
@previous_token = previous_token
@global_config = global_config
end

# Returns the sub-list of all options filtered by a common prefix
Expand All @@ -14,7 +15,7 @@ def initialize(file_path, app_name, current_token, previous_token)
def extract
if File.exists?(file_path)
eval File.read(file_path), binding, __FILE__, __LINE__
TabTab::Definition[app_name].extract_completions(previous_token, current_token)
TabTab::Definition[app_name].extract_completions(previous_token, current_token, global_config)
else
[]
end
Expand Down
7 changes: 4 additions & 3 deletions lib/tabtab/completions/gems.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
class TabTab::Completions::Gem
attr_reader :gem_name, :app_name, :current_token, :previous_token
attr_reader :gem_name, :app_name, :current_token, :previous_token, :global_config

def initialize(gem_name, app_name, current_token, previous_token)
def initialize(gem_name, app_name, current_token, previous_token, global_config = {})
@gem_name = gem_name
@app_name = app_name
@current_token = current_token
@previous_token = previous_token
@global_config = global_config
end

# Returns the sub-list of all options filtered by a common prefix
Expand All @@ -16,7 +17,7 @@ def extract
require "tabtab/definitions"
if definitions_file = load_gem_and_return_definitions_file
eval File.read(definitions_file), binding, __FILE__, __LINE__
TabTab::Definition[app_name].extract_completions(previous_token, current_token)
TabTab::Definition[app_name].extract_completions(previous_token, current_token, global_config)
else
[]
end
Expand Down
9 changes: 7 additions & 2 deletions lib/tabtab/definitions/root.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ def initialize(app_name, options = {}, &block)
import_help_flags(options[:import]) if options[:import]
end

def extract_completions(previous_token, current_token)
def extract_completions(previous_token, current_token, global_config = {})
@current_token = current_token
@global_config = global_config
current = find_active_definition_for_last_token(previous_token) || self
current = (current.parent || self) if current.tokens_consumed == 1
completions = current.filtered_completions(current_token)
Expand All @@ -22,7 +23,7 @@ def extract_completions(previous_token, current_token)
if token =~ /^--/
mem[:long] << token
elsif token =~ /^-/
mem[:short] << token
mem[:short] << token unless hide_short_flags?
else
mem[:command] << token
end
Expand Down Expand Up @@ -61,5 +62,9 @@ def import_help_flags(help_flag)
self.flag(flag.to_sym)
end
end

def hide_short_flags?
@global_config[:shortflags] == 'disable'
end
end
end

0 comments on commit 4e3e35d

Please sign in to comment.