Class: Envo::CmdList

Inherits:
Object
  • Object
show all
Defined in:
lib/envo/cmd_list.rb

Constant Summary collapse

Name =
'list'

Class Method Summary collapse

Class Method Details

.parse_cli(args) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/envo/cmd_list.rb', line 22

def self.parse_cli(args)
  opts = CliParser.filter_opts_front(args)
  raise Envo::Error.new "list: missing name. Use 'list <name> <cmd> <args>'" if args.empty?
  name = args.shift
  return ParsedCmd.new(CmdShow.new([name], true), opts) if args.empty? # just list <name>

  cmd = args.shift
  return CmdListAdd.parse_cli_args(name, args, opts) if cmd == 'add'
  return CmdListDel.parse_cli_args(name, args, opts) if cmd == 'del'
  if cmd == 'clean'
    opts += CliParser.filter_opts_back(args)
    raise Envo::Error.new "list-clean: no args needed. Use 'list <name> clean'" if !args.empty?
    return CmdClean.parse_tokens([name], opts)
  end

  raise Envo::Error.new "list: unkonwn subcommand #{cmd}"
end

.parse_script(tokens, opts) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/envo/cmd_list.rb', line 44

def self.parse_script(tokens, opts)
  raise Envo::Error.new "list: missing name. Use 'list <name> <cmd> <args>'" if tokens.empty?
  name = tokens.shift
  return ParsedCmd.new(CmdShow.new([name], true), opts) if tokens.empty? # just list <name>

  cmd = tokens.shift
  return CmdListAdd.parse_script(name, tokens, opts) if cmd == 'add'
  return CmdListDel.parse_tokens(name, tokens, opts) if cmd == 'del'
  return CmdClean.parse_tokens([name], opts) if cmd == 'clean'

  raise Envo::Error.new "list: unkonwn subcommand #{cmd}"
end

.register_cli_parser(parser) ⇒ Object



18
19
20
# File 'lib/envo/cmd_list.rb', line 18

def self.register_cli_parser(parser)
  parser.add_cmd(Name, ->(cmd, args) { parse_cli(args) })
end

.register_help(help) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/envo/cmd_list.rb', line 4

def self.register_help(help)
  help.add_cmd "list <name>", "show value of list environment variable which is a list"
  help.add_cmd "list <name> add <val>", <<~EOF
    add value to list if it's not already inside
      --front - adds value to front of list (moves it to front if it's already inside)
      --back  - adds value to back of list (moves it to back if it's already inside)
  EOF
  help.add_cmd "list <name> del <val|index>", <<~EOF
    remove a value from a list
    if the provided value is an integer, it's interpreted as an index in the list
  EOF
  help.add_cmd "list <name> clean", "the same as 'clean <name>' (convenience command)"
end

.register_script_parser(parser) ⇒ Object



40
41
42
# File 'lib/envo/cmd_list.rb', line 40

def self.register_script_parser(parser)
  parser.add_cmd(Name, ->(cmd, tokens, opts) { parse_script(tokens, opts) })
end