Module: Tabry::CLI::AllInOne

Defined in:
lib/tabry/cli/all_in_one.rb

Defined Under Namespace

Classes: AllInOneBase

Class Method Summary collapse

Class Method Details

.build(cli: nil, config: nil, &blk) ⇒ Object

Instead of passing in config, you can also run ‘config` with a block or a config option in inside blk. Doing this will also enable the “run completion fast” (see config method)



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/tabry/cli/all_in_one.rb', line 79

def self.build(cli: nil, config: nil, &blk)
  # def self.build(cli: nil, config: nil, &blk)
  cli ||= Class.new(AllInOneBase)

  # If block given. run it to define the CLI methods and/or setting the config
  # with a call to AllInOneBase.config
  if blk
    run_completion = catch(:run_completion) do
      cli.module_eval(&blk)
      nil
    end
  end

  config ||= cli.instance_variable_get(:@tabry_all_in_one_config)

  # If we recognize command is going to be a completion command, fast track and
  # run completion now
  if run_completion && (ARGV.first == "completion")
    fish_mode = ENV.fetch("TABRY_FISH_MODE", false)
    if fish_mode
      require_relative "../shells/fish/wrapper"
      Tabry::Fish::Wrapper.run(ARGV[1], ARGV[2], config: config)
    else
      require_relative "../shells/bash/wrapper"
      Tabry::Bash::Wrapper.run(ARGV[1], ARGV[2], config: config)
    end
    exit
  end

  # If we recognize there is a "completion" subcommand, add completion
  # methods -- if not already defined by caller in the block
  if config.main.subs.by_name["completion"] && !cli.instance_methods.include?(:completion__bash) && !cli.instance_methods.include?(:completion__fish)
    define_completion_methods(cli, config)
  end

  Tabry::CLI::Builder.new(config, cli)
end

.completion_only(completion_conf = nil, **opts, &cmd_conf_blk) ⇒ Object

Creates and runs a CLI whose only purpose is to create a completion wrapper for another program



36
37
38
39
40
41
42
43
44
# File 'lib/tabry/cli/all_in_one.rb', line 36

def self.completion_only(completion_conf = nil, **opts, &cmd_conf_blk)
  completion_conf ||= Tabry::ConfigBuilder.build(**opts, &cmd_conf_blk)
  cmd_name = completion_conf.cmd or raise "cmd is mandatory for completion_only configs"

  cli_class = Class.new(Tabry::CLI::Base)
  cli_conf = Tabry::ConfigBuilder.build { completion }
  define_completion_methods(cli_class, completion_conf, cmd_name: cmd_name)
  Tabry::CLI::Builder.new(cli_conf, cli_class).run(ARGV)
end

.define_completion_methods(cli_class, config, cmd_name: nil) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/tabry/cli/all_in_one.rb', line 46

def self.define_completion_methods(cli_class, config, cmd_name: nil)
  cli_class.module_eval do
    define_method :completion__json do
      require "json"
      # Result is flattened/includes squashed (ConfigList#to_a I think calls flatten), so remove the includes
      puts config.as_json.except(:arg_includes, :option_includes).to_json
    end

    define_method :completion__bash do
      require_relative "../shells/bash"
      Kernel.puts Tabry::Shells::Bash.generate_self(cmd_name: cmd_name)
    end

    define_method :completion__fish do
      require_relative "../shells/fish"
      Kernel.puts Tabry::Shells::Fish.generate_self(cmd_name: cmd_name)
    end

    define_method :completion do
      fish_mode = ENV.fetch("TABRY_FISH_MODE", false)
      if fish_mode
        require_relative "../shells/fish/wrapper"
        Tabry::Fish::Wrapper.run(args.cmd_line, args.comp_point, config: config)
      else
        require_relative "../shells/bash/wrapper"
        Tabry::Bash::Wrapper.run(args.cmd_line, args.comp_point, config: config)
      end
    end
  end
end

.runObject



117
118
119
# File 'lib/tabry/cli/all_in_one.rb', line 117

def self.run(...)
  build(...).run(ARGV)
end