[PATCH 1/3] BZ# 768507: aeolus-cli needs syntax to be more flexible

Richard Su rwsu at redhat.com
Tue Dec 20 19:07:14 UTC 2011


On 12/20/2011 09:30 AM, Jason Guiditta wrote:
> https://bugzilla.redhat.com/show_bug.cgi?id=768507
>
> Refactor cli usage to always require a command.
> ---
>   bin/aeolus-cli                          |    4 +-
>   lib/aeolus_cli.rb                       |    1 +
>   lib/aeolus_cli/command/cli_parser.rb    |   72 +++++++++++++++++++++++++++++++
>   lib/aeolus_cli/command/config_parser.rb |    6 +-
>   spec/command/cli_parser.rb              |   50 +++++++++++++++++++++
>   spec/spec_helper.rb                     |   18 +-------
>   6 files changed, 129 insertions(+), 22 deletions(-)
>   create mode 100644 lib/aeolus_cli/command/cli_parser.rb
>   create mode 100644 spec/command/cli_parser.rb
>
> diff --git a/bin/aeolus-cli b/bin/aeolus-cli
> index f3af29b..9f9a1aa 100755
> --- a/bin/aeolus-cli
> +++ b/bin/aeolus-cli
> @@ -16,5 +16,5 @@
>
>   $:<<  File.expand_path(File.dirname(__FILE__) + '/../lib')
>   require 'aeolus_cli'
> -parser = Aeolus::CLI::ConfigParser.new(ARGV)
> -puts parser.process
> +parser = Aeolus::CLI::CLIParser.new(ARGV)
> +parser.process
> diff --git a/lib/aeolus_cli.rb b/lib/aeolus_cli.rb
> index a5a6bce..851d8da 100644
> --- a/lib/aeolus_cli.rb
> +++ b/lib/aeolus_cli.rb
> @@ -22,6 +22,7 @@ require File.join(File.dirname(__FILE__), 'aeolus_cli/command', 'import_command'
>   require File.join(File.dirname(__FILE__), 'aeolus_cli/command', 'delete_command')
>   require File.join(File.dirname(__FILE__), 'aeolus_cli/command', 'status_command')
>   require File.join(File.dirname(__FILE__), 'aeolus_cli/command', 'config_parser')
> +require File.join(File.dirname(__FILE__), 'aeolus_cli/command', 'cli_parser')
>
>   require File.join(File.dirname(__FILE__), 'aeolus_cli/model', 'base')
>   require File.join(File.dirname(__FILE__), 'aeolus_cli/model', 'image')
> diff --git a/lib/aeolus_cli/command/cli_parser.rb b/lib/aeolus_cli/command/cli_parser.rb
> new file mode 100644
> index 0000000..2aedef0
> --- /dev/null
> +++ b/lib/aeolus_cli/command/cli_parser.rb
> @@ -0,0 +1,72 @@
> +#   Copyright 2011 Red Hat, Inc.
> +#
> +#   Licensed under the Apache License, Version 2.0 (the "License");
> +#   you may not use this file except in compliance with the License.
> +#   You may obtain a copy of the License at
> +#
> +#       http://www.apache.org/licenses/LICENSE-2.0
> +#
> +#   Unless required by applicable law or agreed to in writing, software
> +#   distributed under the License is distributed on an "AS IS" BASIS,
> +#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
> +#   See the License for the specific language governing permissions and
> +#   limitations under the License.
> +
> +require 'optparse'
> +
> +module Aeolus
> +  module CLI
> +    class CLIParser
> +      COMMANDS = %w(image)
> +      attr_accessor :options, :command, :args
> +
> +      def initialize(argv=[], out=STDOUT )
> +        @args = argv
> +        @out = out
> +        @options = {}
> +      end
> +
> +      def process
> +        # Check for command, then call appropriate Optionparser and initiate
> +        # call to that class.
> +        @command = @args.shift
> +        if COMMANDS.include?(@command)
> +          parse() unless @args.include?('-h')
> +          self.send(@command.to_sym)
> +        else
> +          @args<<  "-h" unless @args.include?('-h')
> +          parse()
> +        end
> +      end
> +
> +      private
> +      def parse()
> +        @optparse ||= OptionParser.new do|opts|
> +          opts.banner = "Usage: aeolus-cli [#{COMMANDS.join('|')}] [subcommand] [general options] [command options]"
> +          opts.separator ""
> +          opts.on( '-h', '--help', 'Get usage information for this tool') do
> +            @out.puts opts
> +          end
> +          opts.separator ""
> +          opts.separator "URL with credentials to Conductor are set in ~/.aeolus-cli"
> +          opts.separator "Conductor URL should point to https://<host_where_conductor_runs>/conductor/api"
> +        end
> +
> +        begin
> +          @optparse.parse(@args)
> +        rescue OptionParser::InvalidOption =>  e
> +          #This is just a wrapper, let the actual commands handle bad options later
> +          return true
> +        rescue OptionParser::MissingArgument =>  e
> +          @out.puts "Warning, #{e.message}"
> +          exit(1)
> +        end
> +      end
> +
> +      def image
> +        parser = Aeolus::CLI::ConfigParser.new(@args)
> +        parser.process
> +      end
> +    end
> +  end
> +end
> diff --git a/lib/aeolus_cli/command/config_parser.rb b/lib/aeolus_cli/command/config_parser.rb
> index 2be3e61..438bb70 100644
> --- a/lib/aeolus_cli/command/config_parser.rb
> +++ b/lib/aeolus_cli/command/config_parser.rb
> @@ -35,7 +35,7 @@ module Aeolus
>           # File.expand_path("~")
>           if COMMANDS.include?(@command)
>             parse(@command)
> -          self.send(@command.to_sym)
> +          self.send(@command.to_sym) unless @args.include?('-h')
>           else
>             @args<<  "-h"
>             puts "Valid command required: \n\n"
> @@ -48,7 +48,7 @@ module Aeolus
>           subcommand = subcommand.to_sym if subcommand
>
>           @optparse ||= OptionParser.new do|opts|
> -          opts.banner = "Usage: aeolus-cli [#{COMMANDS.join('|')}] [general options] [command options]"
> +          opts.banner = "Usage: aeolus-cli image [#{COMMANDS.join('|')}] [general options] [command options]"
>
>             opts.separator ""
>             opts.separator "General options:"
> @@ -221,7 +221,7 @@ module Aeolus
>           end
>
>           begin
> -          @optparse.parse!(@args)
> +          @optparse.parse(@args)
>           rescue OptionParser::InvalidOption
>             puts "Warning: Invalid option"
>             exit(1)
> diff --git a/spec/command/cli_parser.rb b/spec/command/cli_parser.rb
> new file mode 100644
> index 0000000..7efbc06
> --- /dev/null
> +++ b/spec/command/cli_parser.rb
> @@ -0,0 +1,50 @@
> +#   Copyright 2011 Red Hat, Inc.
> +#
> +#   Licensed under the Apache License, Version 2.0 (the "License");
> +#   you may not use this file except in compliance with the License.
> +#   You may obtain a copy of the License at
> +#
> +#       http://www.apache.org/licenses/LICENSE-2.0
> +#
> +#   Unless required by applicable law or agreed to in writing, software
> +#   distributed under the License is distributed on an "AS IS" BASIS,
> +#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
> +#   See the License for the specific language governing permissions and
> +#   limitations under the License.
> +
> +require 'spec_helper'
> +
> +module Aeolus
> +  module CLI
> +    describe CLIParser do
> +      it "should parse the specified command" do
> +          ConfigParser.any_instance.should_receive(:process).with(any_args()).and_return(true)
> +          parser = CLIParser.new(%w(image list --images))
> +          parser.process
> +          parser.command.should == 'image'
> +      end
> +
> +      context "output help" do
> +        before(:each) do
> +          @out = double("out")
> +          @out.should_receive(:puts).with(instance_of(OptionParser)).once
> +        end
> +
> +        it "should output help if no params passed" do
> +          parser = CLIParser.new([], @out)
> +          parser.process
> +        end
> +
> +        it "should output help if -h passed" do
> +          parser = CLIParser.new(%w(-h), @out)
> +          parser.process
> +        end
> +
> +        it "should output help if --help passed" do
> +          parser = CLIParser.new(%w(--help), @out)
> +          parser.process
> +        end
> +      end
> +    end
> +  end
> +end
> diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb
> index 2c3ccd7..d198732 100644
> --- a/spec/spec_helper.rb
> +++ b/spec/spec_helper.rb
> @@ -15,24 +15,8 @@
>   $:<<  File.expand_path(File.join(File.dirname(__FILE__), "../lib/aeolus_cli/command/"))
>   $:<<  File.expand_path(File.join(File.dirname(__FILE__), "../lib/aeolus_cli/model/"))
>
> -require 'rubygems'
> -require 'base'
> -require 'image'
> -require 'build'
> -require 'target_image'
> -require 'provider_image'
> -require 'provider'
> -require 'provider_type'
> -require 'provider_account'
> -require 'config_parser'
> +require 'aeolus_cli'
>   require 'stringio'
> -require 'base_command'
> -require 'list_command'
> -require 'build_command'
> -require 'push_command'
> -require 'import_command'
> -require 'delete_command'
> -require 'status_command'
>
>   require 'vcr_setup'
>
Ack.



More information about the aeolus-devel mailing list