Added clubbed flags and renamed multi-character shorthand arguments (#125)

* Added clubbed flags and renamed multi-character shorthand arguments to not interfere with clubbed flags

* Cleaned up code

* Fixed parsing arguments without dash

* Made -h flag clubbable

* Minor text fixes

* Cleaned up incompatible flags method
This commit is contained in:
Metamist 2017-10-07 13:58:13 +02:00 committed by Athitya Kumar
parent 8dec4115ef
commit 3af107627b
3 changed files with 31 additions and 19 deletions

View file

@ -22,11 +22,11 @@ script:
- ( cd spec/fixtures ; colorls .hidden-file ) | fgrep '.hidden-file'
- colorls -l README.md
- colorls -r
- colorls -sd
- colorls -sf
- colorls --sd
- colorls --sf
- colorls -t
- colorls -h
- colorls -gs
- colorls --gs
- colorls spec/fixtures/symlinks
- colorls README.md
- colorls *

View file

@ -25,7 +25,7 @@ module ColorLS
def ls
return print "\n Nothing to show here\n".colorize(@colors[:empty]) if @contents.empty?
return helplog if @help
helplog if @help
if @tree
print "\n"

View file

@ -14,7 +14,7 @@ module ColorLS
long: flag_given?(%w[-l --long]),
tree: flag_given?(%w[-t --tree]),
help: flag_given?(%w[-h --help]),
git_status: flag_given?(%w[-gs --git-status]),
git_status: flag_given?(%w[--gs --git-status]),
colors: @colors
}
@ -34,7 +34,14 @@ module ColorLS
private
def flag_given?(flags)
flags.any? { |flag| @args.include?(flag) }
return true if flags.any? { |flag| @args.include?(flag) }
clubbable_flags = flags.reject { |flag| flag.start_with?('--') }
.map { |flag| flag[1..-1] }
# Some flags should be not be able to be clubbed with other flags
@args.select { |arg| !arg.start_with?('--') && arg.start_with?('-') }
.any? { |arg| clubbable_flags.any? { |flag| arg.include?(flag) } }
end
def set_color_opts
@ -65,11 +72,11 @@ module ColorLS
end
def fetch_sort_opts
sort_dirs_first = flag_given? %w[-sd --sort-dirs --group-directories-first]
sort_files_first = flag_given? %w[-sf --sort-files]
sort_dirs_first = flag_given? %w[--sd --sort-dirs --group-directories-first]
sort_files_first = flag_given? %w[--sf --sort-files]
if sort_dirs_first && sort_files_first
STDERR.puts "\n Restrain from using -sd and -sf flags together."
STDERR.puts "\n Restrain from using --sd and -sf flags together."
.colorize(@colors[:error])
else
return :files if sort_files_first
@ -81,8 +88,13 @@ module ColorLS
def incompatible_flags?
return '' if @opts[:show].nil? || @opts[:sort].nil?
return 'Restrain from using -t (--tree) and -r (--report) flags together.' if @opts[:tree] && @opts[:report]
return 'Restrain from using -t (--tree) and -a (--all) flags together.' if @opts[:tree] && @opts[:all]
[
['-t (--tree)', @opts[:tree], '-r (--report)', @opts[:report]],
['-t (--tree)', @opts[:tree], '-l (--long)', @opts[:long]],
['-t (--tree)', @opts[:tree], '-a (--all)', @opts[:all]]
].each do |flag1, hasflag1, flag2, hasflag2|
return "Restrain from using #{flag1} and #{flag2} flags together." if hasflag1 && hasflag2
end
nil
end