Class: Warp::Dir::App::CLI
Instance Attribute Summary collapse
-
#argv ⇒ Object
Returns the value of attribute argv.
-
#commander ⇒ Object
Returns the value of attribute commander.
-
#config ⇒ Object
Returns the value of attribute config.
-
#flags ⇒ Object
Returns the value of attribute flags.
-
#opts ⇒ Object
Returns the value of attribute opts.
-
#store ⇒ Object
Returns the value of attribute store.
-
#validated ⇒ Object
Returns the value of attribute validated.
Instance Method Summary collapse
-
#initialize(argv) ⇒ CLI
constructor
A new instance of CLI.
- #run(&block) ⇒ Object
- #validate {|_self| ... } ⇒ Object
Constructor Details
#initialize(argv) ⇒ CLI
Returns a new instance of CLI.
13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/warp/dir/app/cli.rb', line 13 def initialize(argv) self.argv = argv # flags are everything that follows -- and is typically flags for the command. # for example: 'wd ls project-point -- -alF' would extract flags = [ '-alF' ] self.flags = extract_suffix_flags(argv.dup) self.flags.flatten! self.commander = ::Warp::Dir::Commander.instance self.config = ::Warp::Dir::Config.new self.opts = nil end |
Instance Attribute Details
#argv ⇒ Object
Returns the value of attribute argv.
11 12 13 |
# File 'lib/warp/dir/app/cli.rb', line 11 def argv @argv end |
#commander ⇒ Object
Returns the value of attribute commander.
11 12 13 |
# File 'lib/warp/dir/app/cli.rb', line 11 def commander @commander end |
#config ⇒ Object
Returns the value of attribute config.
11 12 13 |
# File 'lib/warp/dir/app/cli.rb', line 11 def config @config end |
#flags ⇒ Object
Returns the value of attribute flags.
11 12 13 |
# File 'lib/warp/dir/app/cli.rb', line 11 def flags @flags end |
#opts ⇒ Object
Returns the value of attribute opts.
11 12 13 |
# File 'lib/warp/dir/app/cli.rb', line 11 def opts @opts end |
#store ⇒ Object
Returns the value of attribute store.
11 12 13 |
# File 'lib/warp/dir/app/cli.rb', line 11 def store @store end |
#validated ⇒ Object
Returns the value of attribute validated.
11 12 13 |
# File 'lib/warp/dir/app/cli.rb', line 11 def validated @validated end |
Instance Method Details
#run(&block) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/warp/dir/app/cli.rb', line 49 def run(&block) validate unless validated? response = process_command yield response if block_given? response rescue Exception => e if config.debug STDERR.puts(e.inspect) STDERR.puts(e.backtrace.join("\n")) end on :error do e..red end end |
#validate {|_self| ... } ⇒ Object
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/warp/dir/app/cli.rb', line 25 def validate self.validated = false no_arguments = argv.empty? commands = shift_non_flag_commands self.opts = parse_with_slop(self.argv) # merge first few non-flag args which we filtered into `commands` hash # merge onto the hash resulting from options # and pass on to override the default config opts for the final config. config.configure(opts.to_hash.merge(commands)) self.store = Warp::Dir::Store.new(config, Warp::Dir::Serializer::Dotfile) config.command = :help if (opts.help? || no_arguments) config.command = :warp if !config.command && config.warp if config[:command] self.validated = true else raise Warp::Dir::Errors::InvalidCommand.new('Unable to determine what command to run.'.red) end yield self if block_given? validated end |