Class: Rundoc::CLIArgumentParser
- Inherits:
-
Object
- Object
- Rundoc::CLIArgumentParser
- Defined in:
- lib/rundoc/cli_argument_parser.rb
Overview
This class is responsible for parsing the command line arguments and generating a Cli instance
Example:
cli = CLIArgumentParser.new(argv: ARGV).to_cli
cli.call
Instance Attribute Summary collapse
-
#argv ⇒ Object
readonly
Returns the value of attribute argv.
-
#env ⇒ Object
readonly
Returns the value of attribute env.
-
#exit_obj ⇒ Object
readonly
Returns the value of attribute exit_obj.
-
#io ⇒ Object
readonly
Returns the value of attribute io.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #call ⇒ Object
-
#initialize(argv:, io: $stderr, env: ENV, exit_obj: Kernel) ⇒ CLIArgumentParser
constructor
A new instance of CLIArgumentParser.
Constructor Details
#initialize(argv:, io: $stderr, env: ENV, exit_obj: Kernel) ⇒ CLIArgumentParser
Returns a new instance of CLIArgumentParser.
17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/rundoc/cli_argument_parser.rb', line 17 def initialize( argv:, io: $stderr, env: ENV, exit_obj: Kernel ) @io = io @env = env @argv = argv @options = {} @exit_obj = exit_obj end |
Instance Attribute Details
#argv ⇒ Object (readonly)
Returns the value of attribute argv.
15 16 17 |
# File 'lib/rundoc/cli_argument_parser.rb', line 15 def argv @argv end |
#env ⇒ Object (readonly)
Returns the value of attribute env.
15 16 17 |
# File 'lib/rundoc/cli_argument_parser.rb', line 15 def env @env end |
#exit_obj ⇒ Object (readonly)
Returns the value of attribute exit_obj.
15 16 17 |
# File 'lib/rundoc/cli_argument_parser.rb', line 15 def exit_obj @exit_obj end |
#io ⇒ Object (readonly)
Returns the value of attribute io.
15 16 17 |
# File 'lib/rundoc/cli_argument_parser.rb', line 15 def io @io end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
15 16 17 |
# File 'lib/rundoc/cli_argument_parser.rb', line 15 def @options end |
Instance Method Details
#call ⇒ Object
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 |
# File 'lib/rundoc/cli_argument_parser.rb', line 30 def call source_file = argv.first if source_file.nil? || source_file == "help" parser.parse! ["--help"] return else parser.parse! argv return if [:exit] end source_path = Pathname(source_file) if !source_path.exist? @io.puts "No such file `#{source_path.}`" exit_obj.exit(1) return elsif !source_path.file? @io.puts "Path is not a file. Expected `#{source_path.}` to be a file, but it was not." exit_obj.exit(1) return end [:io] = io [:source_path] = source_path self end |