Class: Codestrap::Cli
- Inherits:
-
Object
- Object
- Codestrap::Cli
- Defined in:
- lib/codestrap/cli.rb
Defined Under Namespace
Classes: CodestrapPathError
Instance Attribute Summary collapse
-
#argv ⇒ String
Same as ARGV.
-
#command ⇒ String
Calling command.
-
#env ⇒ Hash
Environment variables.
Instance Method Summary collapse
-
#abspath(path = nil) ⇒ String
Absolute path of calling command.
-
#initialize(argv) ⇒ Cli
constructor
A new instance of Cli.
- #options ⇒ Object
Constructor Details
#initialize(argv) ⇒ Cli
Returns a new instance of Cli.
53 54 55 56 57 |
# File 'lib/codestrap/cli.rb', line 53 def initialize argv self.abspath(argv.shift) @command = File.basename self.abspath @argv = argv end |
Instance Attribute Details
#argv ⇒ String
Same as ARGV
14 15 16 |
# File 'lib/codestrap/cli.rb', line 14 def argv @argv end |
#command ⇒ String
Calling command
9 10 11 |
# File 'lib/codestrap/cli.rb', line 9 def command @command end |
#env ⇒ Hash
Environment variables. Defaults to system environment variables
22 23 24 |
# File 'lib/codestrap/cli.rb', line 22 def env @env ||= ENV.to_hash end |
Instance Method Details
#abspath(path = nil) ⇒ String
Absolute path of calling command
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/codestrap/cli.rb', line 32 def abspath path=nil return @abspath if @abspath and not path if path =~ /\// p = File.(path) @abspath = p if File.exist? p else env['PATH'].split(File::PATH_SEPARATOR).each do |d| p = File.join(d, path) next unless d.length > 0 next unless File.exist? p @abspath = File.(p) break end end if @abspath @abspath else raise CodestrapPathError, "Could not find #{path}" end end |
#options ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/codestrap/cli.rb', line 59 def ||= ( = OpenStruct.new .generate = false OptionParser.new do |opts| opts. = "Usage: #{self.command} [-?|-h|--help]" opts.separator '' opts.separator 'Specific options:' opts.on('--version', 'Show version') do puts Codestrap::VERSION exit 0 end opts.on('-?', '-h', '--help', 'Show this message') do puts opts exit 0 end opts.on('-g', '--generate', 'Generate links') do .generate = true end end.parse!(self.argv) ) end |