Sha256: de6d8c52a44a2ab4f254b3b7e3924eb18c2c3a6e6dff7709affc81cc1ff0205c

Contents?: true

Size: 1.21 KB

Versions: 24

Compression:

Stored size: 1.21 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') 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

24 entries across 24 versions & 3 rubygems

Version Path
gli-1.3.2 lib/support/initconfig.rb
gli-1.3.1 lib/support/initconfig.rb
gli-1.3.0 lib/support/initconfig.rb
gli-1.2.6 lib/support/initconfig.rb