whatoption 0.1.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/bin/whatoption +48 -24
  3. data/lib/whatoption.rb +49 -15
  4. metadata +3 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0dceb90256c82b20eb0838c98b9aa75ed1747662d93b96232e20164e1279d3ff
4
- data.tar.gz: 8a4c446c1961099ce494e2c669154d3bbc8d8c80294e4bc179ae6ae619f7d1fb
3
+ metadata.gz: 8ae23eb231b87c00885211c51e42ec78fa2d4b5fac7af36163c86a49478ade52
4
+ data.tar.gz: fa93f8e92fc55b95574830fc323bc854708c00c201a01afd5db802f11f496842
5
5
  SHA512:
6
- metadata.gz: 5baf5062312181ecb72ffff8ac6aefda961c2f499648d4d94657495ad5febc5cdc427102172b2e29497ff2ee381b30638a18970c5bde608622e0fe0aedb553aa
7
- data.tar.gz: 170829ba3140198e7e7bf711b5275450375258b56dca1c0577b87febd13a1ce811a730c16237094055f8c4f615c2fc32fd4047579bfaf6fc01386e8e32087ec3
6
+ metadata.gz: 27824e60eb11034c9e1f4b0f26a8732bb67a68514ce492a5ea1561314aa4ae290ff7f89fed0d6e8ceb53be7a97cacdc36536885d618daf0b213c37d09c99ccaa
7
+ data.tar.gz: e4a563e68423ce659244a67edc9d8542f8b612e6db856746be164f827c84a52fe3b691d88d4363fbfb0525f77f7276e016915e062eb259539f72ae0d5dd69c6e
data/bin/whatoption CHANGED
@@ -1,20 +1,15 @@
1
1
  #!/usr/bin/env ruby
2
2
  # ---------------------------------------------------------------
3
3
  # File : whatoption.rb
4
- # Authors : ccmywish <ccmywish@qq.com>
4
+ # Authors : Aoran Zeng <ccmywish@qq.com>
5
5
  # Created on : <2023-02-06>
6
- # Last modified : <2023-02-07>
6
+ # Last modified : <2023-03-14>
7
7
  # Contributors :
8
8
  #
9
9
  # whatoption:
10
10
  #
11
11
  # Tells you what the option of the command means.
12
12
  #
13
- # ----------
14
- # Changelog:
15
- #
16
- # ~> v0.1.0
17
- # <2023-02-06> Create file
18
13
  # ---------------------------------------------------------------
19
14
 
20
15
  require 'whatoption'
@@ -26,6 +21,9 @@ require 'whatoption'
26
21
  class WhatOption::CliHandler
27
22
 
28
23
  def initialize(argv)
24
+
25
+ @argv = argv
26
+
29
27
  if argv.empty?
30
28
  help || return
31
29
  end
@@ -43,24 +41,20 @@ class WhatOption::CliHandler
43
41
  puts cmd = "git -C #{WhatOption::OPTIONS_DATA_DIR} pull"
44
42
  system cmd
45
43
  return
44
+ when '-l'
45
+ list_options
46
+ else
47
+ get_option
46
48
  end
47
49
 
48
- if argv.size == 1
49
- puts("Please input the option for command '#{argv[0]}'".red) || return
50
- end
51
-
52
- @option = ARGV[-1] # Shouldn't detect where is begin with '-' or '--'
53
- # because some options are just single characters,
54
- # for example, 'tar x', this is to use subcommand as option
55
- # That's reasonable also.
56
- @commands = ARGV[0...-1]
57
-
58
50
  # p option
59
51
  # p commands
60
52
  end
61
53
 
62
54
  # routine
63
55
  def help
56
+ puts("WhatOption v#{WhatOption::VERSION}")
57
+ puts
64
58
  puts <<~HELP
65
59
  Usage:
66
60
 
@@ -68,30 +62,60 @@ class WhatOption::CliHandler
68
62
 
69
63
  Options:
70
64
 
71
- -u Update ~/.whatoption data
72
- -h Print this help
73
- -v Print version
65
+ -u Update ~/.whatoption data
66
+ -l <command> List command options
67
+ -h Print this help
68
+ -v Print version
74
69
 
75
70
  Examples:
76
71
 
77
72
  whatoption ruby -c
78
73
  whatoption tar x
79
74
  whatoption git ls-files -z
75
+ whatoption bash -c
76
+ whatoption bash [ -n
80
77
 
81
78
  HELP
82
79
  end
83
80
 
84
- # method
85
- def work
81
+
82
+ # routine
83
+ def get_option
84
+
85
+ if @argv.size == 1
86
+ puts("Please input the option for command '#{@argv[0]}'".red) || return
87
+ end
88
+
89
+ option = ARGV[-1] # Shouldn't detect where is begin with '-' or '--'
90
+ # because some options are just single characters,
91
+ # for example, 'tar x', this is to use subcommand as option
92
+ # That's reasonable also.
93
+ commands = ARGV[0...-1]
94
+
86
95
  # Because the initializer above will not always finish initializing
87
96
  if (not @commands) || (not @option)
88
97
  exit
89
98
  end
90
- option_info = WhatOption::OptionInfo.new @commands, @option
99
+ option_info = WhatOption::OptionInfo.new commands, option
91
100
  option_info.show
92
101
  end
93
102
 
103
+
104
+ # routine
105
+ def list_options
106
+
107
+ if @argv.size == 1
108
+ puts("Please input the command name after '-l'".red) || return
109
+ end
110
+
111
+ option = nil
112
+ commands = ARGV[1..-1]
113
+ option_info = WhatOption::OptionInfo.new commands, option
114
+ option_info.list
115
+ end
116
+
117
+
94
118
  end
95
119
 
120
+ # main
96
121
  cli = WhatOption::CliHandler.new ARGV
97
- cli.work
data/lib/whatoption.rb CHANGED
@@ -1,23 +1,18 @@
1
1
  # ---------------------------------------------------------------
2
2
  # File : whatoption.rb
3
- # Authors : ccmywish <ccmywish@qq.com>
3
+ # Authors : Aoran Zeng <ccmywish@qq.com>
4
4
  # Created on : <2023-02-06>
5
- # Last modified : <2023-02-07>
5
+ # Last modified : <2023-03-14>
6
6
  #
7
7
  # whatoption:
8
8
  #
9
9
  # Lib file
10
10
  #
11
- # ----------
12
- # Changelog:
13
- #
14
- # ~> v0.1.0
15
- # <2023-02-06> Create file
16
11
  # ---------------------------------------------------------------
17
12
 
18
13
  module WhatOption
19
14
 
20
- VERSION = "0.1.0"
15
+ VERSION = "0.3.0"
21
16
 
22
17
  require 'pathname'
23
18
  OPTIONS_DATA_DIR = Pathname.new File.expand_path("~/.whatoption")
@@ -42,8 +37,9 @@ module WhatOption
42
37
 
43
38
  @file # the target TOML file we will load in
44
39
 
45
- @rec_option # record field in toml file
46
- @rec_desc # record field in toml file
40
+ @rec_usage # record field: the option's usage
41
+ @rec_desc # record field: the option's own meaning
42
+ @rec_func # record field: the function of the option
47
43
 
48
44
  =end
49
45
 
@@ -63,14 +59,25 @@ module WhatOption
63
59
 
64
60
  # routine
65
61
  def load_sheet
62
+ require 'tomlrb'
66
63
  # It's very interesting here, you have to put parentheses then 'commands.join()' is
67
64
  # treated as a string.
68
65
  # Without parentheses, the + is affected by OPTIONS_DATA_DIR,
69
66
  # So first '+'s both side are Pathname
70
67
  # And '+' is to add a '/' to component
71
68
  @file = OPTIONS_DATA_DIR + (@commands.join('/') + ".toml")
69
+
70
+ # if the file not exists, it is maybe in the dir (with its own name)
71
+ # For example:
72
+ # "bash -c" will first search 'ROOT/bash.toml'
73
+ # if not exist, then we will search 'ROOT/bash/bash.toml'
74
+ #
75
+ if ! File.exist? @file
76
+ @file = Pathname.new(@file.to_s.delete_suffix(".toml")) +
77
+ (@commands.last + ".toml")
78
+ end
79
+
72
80
  if File.exist? @file
73
- require 'tomlrb'
74
81
  return Tomlrb.load_file @file
75
82
  else
76
83
  puts "File #@file not exist!"
@@ -79,6 +86,28 @@ module WhatOption
79
86
  end
80
87
 
81
88
 
89
+ # method
90
+ def list
91
+ sheet = load_sheet
92
+ if not sheet
93
+ exit 1
94
+ end
95
+
96
+ option_max_len = sheet.keys.map(&:length).max
97
+ left = option_max_len + 3
98
+
99
+ lmda = -> hash do hash['desc'].length end
100
+ desc_max_len = sheet.values.map(&lmda).max
101
+
102
+ middle = desc_max_len + 3
103
+
104
+ puts "Options for '#{@commands.join}':"
105
+ sheet.each do |key, value|
106
+ printf " %s%s%s\n", key.ljust(left), value['desc'].ljust(middle), value['func']
107
+ end
108
+ end
109
+
110
+
82
111
  # routine
83
112
  def resolve
84
113
  sheet = load_sheet
@@ -89,12 +118,15 @@ module WhatOption
89
118
  record = sheet[@option] ||
90
119
  puts("Error: Option [#@option] is not recorded in #@file".red) ||
91
120
  exit(1)
92
- @rec_option = record['option'] ||
93
- puts("Error: No field 'option' in #@file [#@calling_command]".red) ||
121
+ @rec_usage = record['usage'] ||
122
+ puts("Error: No field 'usage' in #@file [#@calling_command]".red) ||
94
123
  exit(1)
95
124
  @rec_desc = record['desc'] ||
96
125
  puts("Error: No field 'desc' in #@file [#@calling_command]".red) ||
97
126
  exit(1)
127
+ @rec_func = record['func'] ||
128
+ puts("Error: No field 'func' in #@file [#@calling_command]".red) ||
129
+ exit(1)
98
130
  end
99
131
 
100
132
 
@@ -106,12 +138,14 @@ module WhatOption
106
138
  if not @sub_commands.empty?
107
139
  result_cmd += ' ' + @sub_commands.join(' ').blue
108
140
  end
109
- result_cmd += ' ' + @rec_option.magenta
141
+ result_cmd += ' ' + @rec_usage.magenta
110
142
  puts result_cmd
111
143
 
112
144
  # newline description
113
- print "\n "
145
+ print "\n #{@option}: "
114
146
  puts @rec_desc.green
147
+ print "\n "
148
+ puts @rec_func
115
149
  puts
116
150
  end
117
151
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: whatoption
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - ccmywish
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-02-07 00:00:00.000000000 Z
11
+ date: 2023-03-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: tomlrb
@@ -73,7 +73,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
73
73
  - !ruby/object:Gem::Version
74
74
  version: '0'
75
75
  requirements: []
76
- rubygems_version: 3.3.26
76
+ rubygems_version: 3.4.1
77
77
  signing_key:
78
78
  specification_version: 4
79
79
  summary: 'whatoption: What does the option mean?'