Class: DoiExtractor::CommandLineParser
- Inherits:
-
Object
- Object
- DoiExtractor::CommandLineParser
- Defined in:
- lib/doi_extractor/command_line_parser.rb
Instance Attribute Summary collapse
-
#command_parser ⇒ Object
readonly
Returns the value of attribute command_parser.
-
#command_parsers ⇒ Object
readonly
Returns the value of attribute command_parsers.
-
#error ⇒ Object
readonly
Returns the value of attribute error.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#top_level_parser ⇒ Object
readonly
Returns the value of attribute top_level_parser.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize ⇒ CommandLineParser
constructor
A new instance of CommandLineParser.
- #parse(args) ⇒ Object
- #to_s ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
#initialize ⇒ CommandLineParser
Returns a new instance of CommandLineParser.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/doi_extractor/command_line_parser.rb', line 6 def initialize @command_parsers = { 'status' => OptionParser.new do |opts| opts. = "Usage: #{File.basename($0)} status [options]" opts.version = VERSION default_opts(opts) api_opts(opts) path_opts(opts) extract_group_opts(opts) end, 'create' => OptionParser.new do |opts| opts. = "Usage: #{File.basename($0)} create [options]" opts.version = VERSION default_opts(opts) api_opts(opts) opts.on(:REQUIRED, '-v', '--doi-version DOI_VERSION', 'DOI version') do |v| self..doi_version = v end opts.on('-y', '--year YEAR', 'Specify year (defaults to current year)') do |v| self..year = v end opts.on('-E', '--email EMAIL', 'Specify email address to notify when all extracts are complete') do |v| self..email = v end end, 'download' => OptionParser.new do |opts| opts. = "Usage: #{File.basename($0)} download [options]" opts.version = VERSION default_opts(opts) api_opts(opts) path_opts(opts) extract_group_opts(opts) force_opts(opts, 'Downloads') end, 'cancel' => OptionParser.new do |opts| opts. = "Usage: #{File.basename($0)} cancel [options]" opts.version = VERSION default_opts(opts) api_opts(opts) extract_group_opts(opts) force_opts(opts, 'Cancels') end } @top_level_parser = OptionParser.new do |opts| opts.version = VERSION opts. = "Usage: #{File.basename($0)} <COMMAND> [options]\nAvailable Commands: #{@command_parsers.keys.join(', ')}\n\nSee '#{File.basename($0)} <COMMAND> --help' for more information on a specific command" end end |
Instance Attribute Details
#command_parser ⇒ Object (readonly)
Returns the value of attribute command_parser.
4 5 6 |
# File 'lib/doi_extractor/command_line_parser.rb', line 4 def command_parser @command_parser end |
#command_parsers ⇒ Object (readonly)
Returns the value of attribute command_parsers.
4 5 6 |
# File 'lib/doi_extractor/command_line_parser.rb', line 4 def command_parsers @command_parsers end |
#error ⇒ Object (readonly)
Returns the value of attribute error.
4 5 6 |
# File 'lib/doi_extractor/command_line_parser.rb', line 4 def error @error end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
4 5 6 |
# File 'lib/doi_extractor/command_line_parser.rb', line 4 def @options end |
#top_level_parser ⇒ Object (readonly)
Returns the value of attribute top_level_parser.
4 5 6 |
# File 'lib/doi_extractor/command_line_parser.rb', line 4 def top_level_parser @top_level_parser end |
Class Method Details
.parse(args) ⇒ Object
147 148 149 150 151 |
# File 'lib/doi_extractor/command_line_parser.rb', line 147 def parse(args) opts = new opts.parse(args.dup) opts end |
Instance Method Details
#parse(args) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/doi_extractor/command_line_parser.rb', line 62 def parse(args) begin @top_level_parser.order!(args) command = args.shift.to_s.downcase @command_parser = @command_parsers[command] if @command_parser @options = Options.for_command(command) @command_parser.order!(args) if @options.valid? @valid = true else @valid = false @error = @options.errors.join("\n") end else @valid = false @error = 'Invalid Command' end rescue OptionParser::ParseError => err @error = err @valid = false end end |
#to_s ⇒ Object
92 93 94 |
# File 'lib/doi_extractor/command_line_parser.rb', line 92 def to_s [error, @command_parser || @top_level_parser].compact.join("\n\n") end |
#valid? ⇒ Boolean
88 89 90 |
# File 'lib/doi_extractor/command_line_parser.rb', line 88 def valid? @valid end |