Sha256: e6b13ce6e8c602f9ab9efe8a3f1873082dbf3f3d9bb814ba1f8976d06d5a98ef

Contents?: true

Size: 1.22 KB

Versions: 6

Compression:

Stored size: 1.22 KB

Contents

require 'gli'
require 'gli/command'
require 'yaml'

module GLI
  class InitConfig < Command # :nodoc:
    COMMANDS_KEY = 'commands'

    def initialize(config_file_name)
      @filename = config_file_name
      super(:initconfig,"Initialize the config file using current global options",nil,'Initializes a configuration file where you can set default options for command line flags, both globally and on a per-command basis.  These defaults override the built-in defaults and allow you to omit commonly-used command line flags when invoking this program')

      self.desc 'force overwrite of existing config file'
      self.switch :force
    end

    def execute(global_options,options,arguments)
      if options[:force] || !File.exist?(@filename)
        config = global_options
        config[COMMANDS_KEY] = {}
        GLI.commands.each do |name,command|
          if (command != self) && (name != :rdoc) && (name != :help)
            config[COMMANDS_KEY][name.to_sym] = {} if command != self
          end
        end
        File.open(@filename,'w', 0600) do |file|
          YAML.dump(config,file)
        end
      else
        raise "Not overwriting existing config file #{@filename}, use --force to override"
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
gli-1.6.0 lib/support/initconfig.rb
gli-1.5.1 lib/support/initconfig.rb
iisconfig-0.0.1.pre2 build/bundler/ruby/1.9.1/gems/gli-1.4.0/lib/support/initconfig.rb
iisconfig-0.0.1.pre1 build/bundler/ruby/1.9.1/gems/gli-1.4.0/lib/support/initconfig.rb
gli-1.4.0 lib/support/initconfig.rb
gli-1.3.7 lib/support/initconfig.rb