-
-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathcli.rb
140 lines (121 loc) · 4.67 KB
/
cli.rb
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# encoding: utf-8
# frozen_string_literal: true
require 'thor'
require_relative 'licenses'
module TTY
# Main CLI runner
# @api public
class CLI < Thor
extend TTY::Licenses
# Error raised by this runner
Error = Class.new(StandardError)
no_commands do
def self.logo(banner)
<<-EOS
┏━━━┓
┏━┳╋┳┳━┻━━┓
┣━┫┗┫┗┳┳┳━┫
┃ ┃┏┫┏┫┃┃★┃ #{banner}
┃ ┗━┻━╋┓┃ ┃
┗━━━━━┻━┻━┛
EOS
end
def self.top_banner
require 'pastel'
pastel = Pastel.new
pastel.red(logo('Terminal apps toolkit'))
end
def self.executable_name
::File.basename($PROGRAM_NAME)
end
end
class_option :"no-color", type: :boolean, default: false,
desc: 'Disable colorization in output'
class_option :"dry-run", type: :boolean, aliases: ['-r'],
desc: 'Run but do not make any changes'
class_option :debug, type: :boolean, default: false,
desc: 'Run in debug mode'
def self.help(*)
print top_banner
super
end
desc 'add COMMAND [SUBCOMMAND] [OPTIONS]', 'Add a command to the command line app.'
long_desc <<-D
The `teletype add` will create a new command and place it into
appropriate structure in the cli app.
Example:
teletype add config --desc 'Set and get configuration options'
This generates a command in app/commands/config.rb
You can also add subcommands
Example:
teletype add config server
This generates a command in app/commands/config/server.rb
D
method_option :args, type: :array, aliases: '-a', default: [],
desc: 'List command argument names',
banner: 'arg1 arg2'
method_option :desc, aliases: '-d', desc: "Describe command's purpose"
method_option :force, type: :boolean, aliases: '-f',
desc: 'Overwrite existing command'
method_option :help, type: :boolean,
aliases: '-h', desc: 'Display usage information'
method_option :test, type: :string, aliases: '-t',
desc: 'Generate a test setup',
banner: 'rspec', enum: %w(rspec minitest)
def add(*names)
if options[:help]
invoke :help, ['add']
elsif names.size < 1
fail Error, "'teletype add' was called with no arguments\n" \
"Usage: 'teletype add COMMAND_NAME'"
else
require_relative 'commands/add'
TTY::Commands::Add.new(names, options).execute
end
end
desc 'new PROJECT_NAME [OPTIONS]', 'Create a new command line app skeleton.'
long_desc <<-D
The 'teletype new' command creates a new command line application
with a default directory structure and configuration at the
specified path.
The PROJECT_NAME will be the name for the directory that includes all the
files and be the default binary name.
Example:
teletype new cli_app
D
method_option :author, type: :array, aliases: '-a',
desc: 'Author(s) of this library',
banner: 'name1 name2'
method_option :ext, type: :boolean, default: false,
desc: 'Generate a boilerpalate for C extension'
method_option :coc, type: :boolean, default: true,
desc: 'Generate a code of conduct file'
method_option :force, type: :boolean, aliases: '-f',
desc: 'Overwrite existing files'
method_option :help, aliases: '-h', type: :boolean,
desc: 'Display usage information'
method_option :license, type: :string, default: 'mit', banner: 'mit',
aliases: '-l', desc: 'Generate a license file',
enum: licenses.keys.concat(['custom'])
method_option :test, type: :string, default: 'rspec',
aliases: '-t', desc: 'Generate a test setup',
banner: 'rspec', enum: %w(rspec minitest)
def new(app_name = nil)
if options[:help]
invoke :help, ['new']
elsif app_name.nil?
fail Error, "'teletype new' was called with no arguments\n" \
"Usage: 'teletype new PROJECT_NAME'"
else
require_relative 'commands/new'
TTY::Commands::New.new(app_name, options).execute
end
end
desc 'version', 'TTY version'
def version
require_relative 'version'
puts "v#{TTY::VERSION}"
end
map %w(--version -v) => :version
end # CLI
end # TTY