File: commander

package info (click to toggle)
ruby-commander 4.6.0-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 360 kB
  • sloc: ruby: 1,971; makefile: 9
file content (105 lines) | stat: -rwxr-xr-x 3,423 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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
76
77
78
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
#!/usr/bin/env ruby
# frozen_string_literal: true

require 'rubygems'
require 'commander/import'

program :name, 'commander'
program :version, Commander::VERSION
program :description, 'Commander utility program.'

command :init do |c|
  c.syntax = 'commander init [option] <file>'
  c.summary = 'Initialize a commander template'
  c.description = 'Initialize an empty <file> with a commander template,
    allowing very quick creation of commander executables.'
  c.example 'Create a new classic style template file.', 'commander init bin/my_executable'
  c.example 'Create a new modular style template file.', 'commander init --modular bin/my_executable'
  c.option '-m', '--modular', 'Initialize a modular style template'
  c.action do |args, options|
    file = args.shift || abort('file argument required.')
    name = ask 'Machine name of program: '
    description = ask 'Describe your program: '
    commands = ask_for_array 'List the commands you wish to create: '
    begin
      if options.modular
        File.open(file, 'w') do |f|
          f.write <<-"...".gsub!(/^ {10}/, '')
          #!/usr/bin/env ruby

          require 'rubygems'
          require 'commander'

          class MyApplication
            include Commander::Methods
            # include whatever modules you need

            def run
              program :name, '#{name}'
              program :version, '0.0.1'
              program :description, '#{description}'

          ...
          commands.each do |command|
            f.write <<-"...".gsub!(/^ {12}/, '')
                command :#{command} do |c|
                  c.syntax = '#{name} #{command} [options]'
                  c.summary = ''
                  c.description = ''
                  c.example 'description', 'command example'
                  c.option '--some-switch', 'Some switch that does something'
                  c.action do |args, options|
                    # Do something or c.when_called #{name.capitalize}::Commands::#{command.capitalize}
                  end
                end

            ...
          end
          f.write <<-"...".gsub!(/^ {12}/, '')
                run!
              end
            end

            MyApplication.new.run if $0 == __FILE__
          ...
        end

        File.chmod(0755, file)
        say "Initialized modular template in #{file}"
      else
        File.open(file, 'w') do |f|
          f.write <<-"...".gsub!(/^ {10}/, '')
          #!/usr/bin/env ruby

          require 'rubygems'
          require 'commander/import'

          program :name, '#{name}'
          program :version, '0.0.1'
          program :description, '#{description}'

          ...
          commands.each do |command|
            f.write <<-"...".gsub!(/^ {12}/, '')
            command :#{command} do |c|
              c.syntax = '#{name} #{command} [options]'
              c.summary = ''
              c.description = ''
              c.example 'description', 'command example'
              c.option '--some-switch', 'Some switch that does something'
              c.action do |args, options|
                # Do something or c.when_called #{name.capitalize}::Commands::#{command.capitalize}
              end
            end

            ...
          end
        end
        File.chmod 0755, file
        say "Initialized template in #{file}"
      end
    rescue StandardError => e
      abort e
    end
  end
end