Skip to content

Commit

Permalink
Add a global 'require' option for rougify CLI tool (rouge-ruby#1215)
Browse files Browse the repository at this point in the history
This commit adds a global 'require' option for the rougify CLI tool.
This is needed because, for custom extensions (like lexers) to be
loaded, Rouge itself must be loaded first. So the usual method of `ruby
-r <plugin> <binary>` won't work because `Rouge::Lexer` will not be
available for subclassing.
  • Loading branch information
jneen authored and pyrmont committed Jun 26, 2019
1 parent 87fe6a4 commit 3e9f1e1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
3 changes: 3 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ Performance/RedundantBlockCall:
Lint/UselessAssignment:
Enabled: false

Lint/AssignmentInCondition:
AllowSafeAssignment: true

Lint/UnusedMethodArgument:
Enabled: false

Expand Down
29 changes: 18 additions & 11 deletions lib/rouge/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class CLI
def self.doc
return enum_for(:doc) unless block_given?

yield %|usage: rougify [command] [args...]|
yield %|usage: rougify {global options} [command] [args...]|
yield %||
yield %|where <command> is one of:|
yield %| highlight #{Highlight.desc}|
Expand All @@ -48,6 +48,9 @@ def self.doc
yield %| guess #{Guess.desc}|
yield %| version #{Version.desc}|
yield %||
yield %|global options:|
yield %[ --require|-r <fname> require <fname> after loading rouge]
yield %||
yield %|See `rougify help <command>` for more info.|
end

Expand All @@ -62,18 +65,22 @@ def initialize(message, status=1)
def self.parse(argv=ARGV)
argv = normalize_syntax(argv)

mode = argv.shift
while (head = argv.shift)
case head
when '-h', '--help', 'help', '-help'
return Help.parse(argv)
when '--require', '-r'
require argv.shift
else
break
end
end

klass = class_from_arg(mode)
klass = class_from_arg(head)
return klass.parse(argv) if klass

case mode
when '-h', '--help', 'help', '-help', nil
Help.parse(argv)
else
argv.unshift(mode) if mode
Highlight.parse(argv)
end
argv.unshift(head) if head
Highlight.parse(argv)
end

def initialize(options={})
Expand All @@ -91,7 +98,7 @@ def self.class_from_arg(arg)
case arg
when 'version', '--version', '-v'
Version
when 'help'
when 'help', nil
Help
when 'highlight', 'hi'
Highlight
Expand Down

0 comments on commit 3e9f1e1

Please sign in to comment.