Module: UniversaTools::Commons::CliCommands

Defined in:
lib/universa_tools/commons.rb

Defined Under Namespace

Classes: Command

Instance Method Summary collapse

Instance Method Details

#cmd(name1, name2, description = nil, &block) ⇒ Object



83
84
85
86
87
88
89
90
91
# File 'lib/universa_tools/commons.rb', line 83

def cmd(name1, name2, description = nil, &block)
  if !description
    description = name2
    name2 = nil
  end
  command = Command.new(name1, name2, description, block)
  @commands[name1] = command
  @commands[name2] = command if name2
end

#cmd_listObject



93
94
95
96
# File 'lib/universa_tools/commons.rb', line 93

def cmd_list
  puts "Listing contents of #@keyring_path:"
  keyring
end

#init_commandsObject



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/universa_tools/commons.rb', line 98

def init_commands
  cmd("help", "help on supported commands. To get extended help on some command, use 'help <command>'.") { |args|
    STDOUT.puts @option_parser.banner
    puts ""
    if args.empty?
      puts "#{ANSI.bold { "Available commands:" }}\n\n"
      cc = @commands.values.uniq
      first_column_size = cc.map { |x| x.name.size }.max + 2
      cc.each { |cmd|
        description_lines =
            first_spacer = ANSI.bold { ANSI.yellow { "%#{-first_column_size}s" % cmd.name } }
        next_spacer = ' ' * first_column_size
        cmd.description.word_wrap(@term_width - first_column_size).lines.each_with_index { |s, n|
          STDOUT << (n == 0 ? first_spacer : next_spacer)
          STDOUT.puts s
        }

      }
    else
      puts "-- not yet ready"
    end
  }

  cmd("list", "l", "show contents of the keyring")
end