Skip to content

Commit 7675f47

Browse files
author
Chris Hunt
committed
Use OptionParser for parsing command line options
1 parent f18fd8d commit 7675f47

6 files changed

Lines changed: 90 additions & 41 deletions

File tree

Rakefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ desc 'Check code quality'
1212
Cane::RakeTask.new(:quality) do |task|
1313
task.abc_max = 9
1414
task.use Cane::HashCheck
15+
task.abc_exclude = %w(Auth::Options#initialize)
1516
end
1617

1718
task default: :spec

bin/gh-auth

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
require 'github/auth'
44

5-
Github::Auth::CLI.new(ARGV).execute
5+
Github::Auth::CLI.new.execute(ARGV)

lib/github/auth.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
require 'github/auth/key'
33
require 'github/auth/keys_client'
44
require 'github/auth/keys_file'
5+
require 'github/auth/options'
56
require 'github/auth/cli'

lib/github/auth/cli.rb

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,21 @@
11
module Github::Auth
22
# Command Line Interface for parsing and executing commands
33
class CLI
4-
attr_reader :command, :usernames
4+
attr_reader :options
55

6-
COMMANDS = %w(add remove list)
7-
8-
def initialize(argv)
9-
@command = argv.shift
10-
@usernames = argv
11-
end
12-
13-
def execute
14-
if COMMANDS.include?(command)
15-
send command
16-
elsif command == '--version'
17-
puts "gh-auth version #{Github::Auth::VERSION}"
18-
else
19-
print_usage
20-
end
6+
def execute(args)
7+
@options = Options.new.parse(args)
8+
send options.command
219
end
2210

2311
private
2412

2513
def add
26-
if usernames.empty?
27-
print_usage
28-
return
29-
end
30-
3114
on_keys_file :write!,
3215
"Adding #{keys.count} key(s) to '#{keys_file.path}'"
3316
end
3417

3518
def remove
36-
if usernames.empty?
37-
print_usage
38-
return
39-
end
40-
4119
on_keys_file :delete!,
4220
"Removing #{keys.count} key(s) from '#{keys_file.path}'"
4321
end
@@ -46,6 +24,14 @@ def list
4624
puts "Added users: #{keys_file.github_users.join(', ')}"
4725
end
4826

27+
def version
28+
puts Github::Auth::VERSION
29+
end
30+
31+
def usage
32+
puts options.usage
33+
end
34+
4935
def on_keys_file(action, message)
5036
puts message
5137
rescue_keys_file_errors { keys_file.send action, keys }
@@ -65,12 +51,8 @@ def rescue_keys_file_errors
6551
puts " $ touch #{keys_file.path}"
6652
end
6753

68-
def print_usage
69-
puts "usage: gh-auth [--version] [#{COMMANDS.join '|'}] <username>"
70-
end
71-
7254
def keys
73-
@keys ||= usernames.map { |username| keys_for username }.flatten.compact
55+
@keys ||= options.usernames.map { |user| keys_for user }.flatten.compact
7456
end
7557

7658
def keys_for(username)

lib/github/auth/options.rb

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
require 'optparse'
2+
3+
module Github::Auth
4+
# Parses command line options
5+
class Options
6+
attr_reader :parser
7+
8+
def initialize
9+
@parser = OptionParser.new do |opts|
10+
opts.banner = [
11+
'usage: gh-auth',
12+
'[--version]',
13+
'[add|remove|list]',
14+
'<username>'
15+
].join(' ')
16+
17+
opts.separator "\noptions:"
18+
19+
opts.on(
20+
'--add doug,sally', Array,
21+
"GitHub user(s) you'd like to add"
22+
) do |usernames|
23+
raise OptionParser::MissingArgument if usernames.empty?
24+
@command = 'add'
25+
@usernames = usernames
26+
end
27+
28+
opts.on(
29+
'--remove doug,sally', Array,
30+
"GitHub user(s) you'd like to remove"
31+
) do |usernames|
32+
raise OptionParser::MissingArgument if usernames.empty?
33+
@command = 'remove'
34+
@usernames = usernames
35+
end
36+
37+
opts.on('--list', "List all GitHub users you've added") do
38+
@command = 'list'
39+
end
40+
41+
opts.on('--version', 'Show version') do
42+
@command = 'version'
43+
end
44+
end
45+
end
46+
47+
def command
48+
@command ||= 'usage'
49+
end
50+
51+
def usernames
52+
@usernames ||= []
53+
end
54+
55+
def usage
56+
parser.help
57+
end
58+
59+
def parse(args)
60+
parser.parse(args)
61+
ensure
62+
return self
63+
end
64+
end
65+
end

spec/acceptance/github/auth/cli_spec.rb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111

1212
after { keys_file.unlink }
1313

14-
def cli(argv)
15-
described_class.new(argv).tap do |cli|
14+
def cli
15+
described_class.new.tap do |cli|
1616
cli.stub(
1717
github_hostname: hostname,
1818
keys_file_path: keys_file.path
@@ -21,41 +21,41 @@ def cli(argv)
2121
end
2222

2323
it 'adds and removes keys from the keys file' do
24-
cli(%w(add chrishunt)).execute
24+
cli.execute %w(--add chrishunt)
2525

2626
keys_file.read.tap do |keys_file_content|
2727
keys.each { |key| expect(keys_file_content).to include key.to_s }
2828
end
2929

30-
cli(%w(remove chrishunt)).execute
30+
cli.execute %w(--remove chrishunt)
3131

3232
expect(keys_file.read).to be_empty
3333

3434
keys_file.unlink
3535
end
3636

3737
it 'lists users from the keys file' do
38-
cli(%w(add chrishunt)).execute
38+
cli.execute %w(--add chrishunt)
3939

4040
output = capture_stdout do
41-
cli(%w(list)).execute
41+
cli.execute %w(--list)
4242
end
4343

4444
expect(output).to include('chrishunt')
4545
end
4646

4747
it 'prints version information' do
4848
output = capture_stdout do
49-
cli(%w(--version)).execute
49+
cli.execute %w(--version)
5050
end
5151

5252
expect(output).to include Github::Auth::VERSION
5353
end
5454

5555
it 'prints usage for invalid arguments' do
56-
[[], %w(invalid), %w(add)].each do |invalid_arguments|
56+
[[], %w(invalid), %w(--add)].each do |invalid_arguments|
5757
expect(
58-
capture_stdout { cli(invalid_arguments).execute }
58+
capture_stdout { cli.execute invalid_arguments }
5959
).to include 'usage: gh-auth'
6060
end
6161
end

0 commit comments

Comments
 (0)