Class: VScripts::CommandLine
- Inherits:
- 
      Object
      
        - Object
- VScripts::CommandLine
 
- Defined in:
- lib/vscripts/command_line.rb
Overview
Parses the command line arguments
Instance Attribute Summary collapse
- 
  
    
      #command  ⇒ String 
    
    
  
  
  
  
    
      readonly
    
    
  
  
  
  
  
  
    The command name. 
- 
  
    
      #command_options  ⇒ Array 
    
    
  
  
  
  
    
      readonly
    
    
  
  
  
  
  
  
    The command specific arguments. 
- 
  
    
      #global  ⇒ Hash 
    
    
  
  
  
  
    
      readonly
    
    
  
  
  
  
  
  
    The global command line arguments. 
Instance Method Summary collapse
- 
  
    
      #initialize(args = ARGV)  ⇒ Hash 
    
    
  
  
  
    constructor
  
  
  
  
  
  
  
    The command line options. 
- 
  
    
      #parse_command(args)  ⇒ String, Array 
    
    
  
  
  
  
  
  
  
  
  
    Ensures command is available. 
- 
  
    
      #parse_global(args)  ⇒ Hash 
    
    
  
  
  
  
  
  
  
  
  
    The command line options. 
- 
  
    
      #parser  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Specifies command line options This method smells of :reek:NestedIterators but ignores them This method smells of :reek:TooManyStatements but ignores them. 
Constructor Details
#initialize(args = ARGV) ⇒ Hash
Returns the command line options.
| 17 18 19 | # File 'lib/vscripts/command_line.rb', line 17 def initialize(args = ARGV) parse_global(args) end | 
Instance Attribute Details
#command ⇒ String (readonly)
Returns the command name.
| 11 12 13 | # File 'lib/vscripts/command_line.rb', line 11 def command @command end | 
#command_options ⇒ Array (readonly)
Returns the command specific arguments.
| 13 14 15 | # File 'lib/vscripts/command_line.rb', line 13 def @command_options end | 
#global ⇒ Hash (readonly)
Returns the global command line arguments.
| 9 10 11 | # File 'lib/vscripts/command_line.rb', line 9 def global @global end | 
Instance Method Details
#parse_command(args) ⇒ String, Array
Ensures command is available
| 62 63 64 65 66 67 68 69 | # File 'lib/vscripts/command_line.rb', line 62 def parse_command(args) command = args.shift return unless command command_class = command.capitalize.to_sym abort "Error: unknown subcommand '#{command}'\nTry --help." \ unless Commands.list.include?(command_class) @command, @command_options = [command_class, args] end | 
#parse_global(args) ⇒ Hash
Returns the command line options.
| 51 52 53 54 55 56 57 | # File 'lib/vscripts/command_line.rb', line 51 def parse_global(args) Trollop.with_standard_exception_handling parser do @global = parser.parse args fail Trollop::HelpNeeded if args.empty? || !parse_command(args) end @global end | 
#parser ⇒ Object
Specifies command line options This method smells of :reek:NestedIterators but ignores them This method smells of :reek:TooManyStatements but ignores them
| 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | # File 'lib/vscripts/command_line.rb', line 24 def parser # rubocop:disable MethodLength available = Commands.list.map { |cmd| cmd.to_s.downcase } Trollop::Parser.new do version VScripts::VERSION::COPYRIGHT <<-EOS VScripts automation daemon. Available commands: #{available} USAGE: vscripts GLOBAL-OPTIONS COMMAND OPTIONS For help on an individual command: vscripts COMMAND --help GLOBAL OPTIONS: EOS opt :config, 'Specify configuration file', type: :string, short: '-c' stop_on available stop_on_unknown end end |