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.
12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/warp/dir/app/cli.rb', line 12 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.
10 11 12 |
# File 'lib/warp/dir/app/cli.rb', line 10 def argv @argv end |
#commander ⇒ Object
Returns the value of attribute commander.
10 11 12 |
# File 'lib/warp/dir/app/cli.rb', line 10 def commander @commander end |
#config ⇒ Object
Returns the value of attribute config.
10 11 12 |
# File 'lib/warp/dir/app/cli.rb', line 10 def config @config end |
#flags ⇒ Object
Returns the value of attribute flags.
10 11 12 |
# File 'lib/warp/dir/app/cli.rb', line 10 def flags @flags end |
#opts ⇒ Object
Returns the value of attribute opts.
10 11 12 |
# File 'lib/warp/dir/app/cli.rb', line 10 def opts @opts end |
#store ⇒ Object
Returns the value of attribute store.
10 11 12 |
# File 'lib/warp/dir/app/cli.rb', line 10 def store @store end |
#validated ⇒ Object
Returns the value of attribute validated.
10 11 12 |
# File 'lib/warp/dir/app/cli.rb', line 10 def validated @validated end |
Instance Method Details
#run(&block) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/warp/dir/app/cli.rb', line 50 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
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 |
# File 'lib/warp/dir/app/cli.rb', line 24 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.warp || config.command == 'warp' String.disable_colors if config.no_color 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 |