whatoption 0.1.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 +7 -0
  2. data/bin/whatoption +97 -0
  3. data/lib/whatoption.rb +121 -0
  4. metadata +80 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 0dceb90256c82b20eb0838c98b9aa75ed1747662d93b96232e20164e1279d3ff
4
+ data.tar.gz: 8a4c446c1961099ce494e2c669154d3bbc8d8c80294e4bc179ae6ae619f7d1fb
5
+ SHA512:
6
+ metadata.gz: 5baf5062312181ecb72ffff8ac6aefda961c2f499648d4d94657495ad5febc5cdc427102172b2e29497ff2ee381b30638a18970c5bde608622e0fe0aedb553aa
7
+ data.tar.gz: 170829ba3140198e7e7bf711b5275450375258b56dca1c0577b87febd13a1ce811a730c16237094055f8c4f615c2fc32fd4047579bfaf6fc01386e8e32087ec3
data/bin/whatoption ADDED
@@ -0,0 +1,97 @@
1
+ #!/usr/bin/env ruby
2
+ # ---------------------------------------------------------------
3
+ # File : whatoption.rb
4
+ # Authors : ccmywish <ccmywish@qq.com>
5
+ # Created on : <2023-02-06>
6
+ # Last modified : <2023-02-07>
7
+ # Contributors :
8
+ #
9
+ # whatoption:
10
+ #
11
+ # Tells you what the option of the command means.
12
+ #
13
+ # ----------
14
+ # Changelog:
15
+ #
16
+ # ~> v0.1.0
17
+ # <2023-02-06> Create file
18
+ # ---------------------------------------------------------------
19
+
20
+ require 'whatoption'
21
+
22
+ ####################
23
+ # main
24
+ ####################
25
+
26
+ class WhatOption::CliHandler
27
+
28
+ def initialize(argv)
29
+ if argv.empty?
30
+ help || return
31
+ end
32
+
33
+ case argv[0]
34
+ when '-h'
35
+ help || return
36
+ when '-v'
37
+ puts("WhatOption v#{WhatOption::VERSION}") || return
38
+ when '-u'
39
+ # This output will be hided into the return string
40
+ # `git -C #{WhatOption::OPTIONS_DATA_DIR} pull`
41
+
42
+ # So we use system()
43
+ puts cmd = "git -C #{WhatOption::OPTIONS_DATA_DIR} pull"
44
+ system cmd
45
+ return
46
+ end
47
+
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
+ # p option
59
+ # p commands
60
+ end
61
+
62
+ # routine
63
+ def help
64
+ puts <<~HELP
65
+ Usage:
66
+
67
+ whatoption command subcommands option
68
+
69
+ Options:
70
+
71
+ -u Update ~/.whatoption data
72
+ -h Print this help
73
+ -v Print version
74
+
75
+ Examples:
76
+
77
+ whatoption ruby -c
78
+ whatoption tar x
79
+ whatoption git ls-files -z
80
+
81
+ HELP
82
+ end
83
+
84
+ # method
85
+ def work
86
+ # Because the initializer above will not always finish initializing
87
+ if (not @commands) || (not @option)
88
+ exit
89
+ end
90
+ option_info = WhatOption::OptionInfo.new @commands, @option
91
+ option_info.show
92
+ end
93
+
94
+ end
95
+
96
+ cli = WhatOption::CliHandler.new ARGV
97
+ cli.work
data/lib/whatoption.rb ADDED
@@ -0,0 +1,121 @@
1
+ # ---------------------------------------------------------------
2
+ # File : whatoption.rb
3
+ # Authors : ccmywish <ccmywish@qq.com>
4
+ # Created on : <2023-02-06>
5
+ # Last modified : <2023-02-07>
6
+ #
7
+ # whatoption:
8
+ #
9
+ # Lib file
10
+ #
11
+ # ----------
12
+ # Changelog:
13
+ #
14
+ # ~> v0.1.0
15
+ # <2023-02-06> Create file
16
+ # ---------------------------------------------------------------
17
+
18
+ module WhatOption
19
+
20
+ VERSION = "0.1.0"
21
+
22
+ require 'pathname'
23
+ OPTIONS_DATA_DIR = Pathname.new File.expand_path("~/.whatoption")
24
+
25
+ if not Dir.exist? OPTIONS_DATA_DIR
26
+ `git clone https://github.com/ccmywish/whatoption.toml #{OPTIONS_DATA_DIR}`
27
+ end
28
+
29
+
30
+ class OptionInfo
31
+
32
+ require 'colorator'
33
+
34
+ =begin
35
+
36
+ attribute:
37
+
38
+ @commands # all commands(including sub)
39
+ @calling_command # the calling command itself
40
+ @sub_commands # possible sub commands
41
+ @option # the option we query
42
+
43
+ @file # the target TOML file we will load in
44
+
45
+ @rec_option # record field in toml file
46
+ @rec_desc # record field in toml file
47
+
48
+ =end
49
+
50
+
51
+ # method
52
+ def initialize(commands, option)
53
+ # Must clone, because we will delete in place later
54
+ @commands = commands.clone
55
+ @calling_command = commands[0]
56
+
57
+ commands.delete_at(0)
58
+ @sub_commands = commands
59
+
60
+ @option = option
61
+ end
62
+
63
+
64
+ # routine
65
+ def load_sheet
66
+ # It's very interesting here, you have to put parentheses then 'commands.join()' is
67
+ # treated as a string.
68
+ # Without parentheses, the + is affected by OPTIONS_DATA_DIR,
69
+ # So first '+'s both side are Pathname
70
+ # And '+' is to add a '/' to component
71
+ @file = OPTIONS_DATA_DIR + (@commands.join('/') + ".toml")
72
+ if File.exist? @file
73
+ require 'tomlrb'
74
+ return Tomlrb.load_file @file
75
+ else
76
+ puts "File #@file not exist!"
77
+ nil
78
+ end
79
+ end
80
+
81
+
82
+ # routine
83
+ def resolve
84
+ sheet = load_sheet
85
+ if not sheet
86
+ exit 1
87
+ end
88
+
89
+ record = sheet[@option] ||
90
+ puts("Error: Option [#@option] is not recorded in #@file".red) ||
91
+ exit(1)
92
+ @rec_option = record['option'] ||
93
+ puts("Error: No field 'option' in #@file [#@calling_command]".red) ||
94
+ exit(1)
95
+ @rec_desc = record['desc'] ||
96
+ puts("Error: No field 'desc' in #@file [#@calling_command]".red) ||
97
+ exit(1)
98
+ end
99
+
100
+
101
+ # method
102
+ def show
103
+ resolve
104
+
105
+ result_cmd = @calling_command.yellow
106
+ if not @sub_commands.empty?
107
+ result_cmd += ' ' + @sub_commands.join(' ').blue
108
+ end
109
+ result_cmd += ' ' + @rec_option.magenta
110
+ puts result_cmd
111
+
112
+ # newline description
113
+ print "\n "
114
+ puts @rec_desc.green
115
+ puts
116
+ end
117
+
118
+ end
119
+
120
+
121
+ end
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: whatoption
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - ccmywish
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-02-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: tomlrb
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: colorator
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.1'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.1'
41
+ description: 'This command line tool `whatoption` is used to record and explain commands''
42
+ options, so there is no need to run the command itself to check what the help info
43
+ is. Besides, many options only have one character, which is sometimes not easily
44
+ understood by users to remember.
45
+
46
+ '
47
+ email: ccmywish@qq.com
48
+ executables:
49
+ - whatoption
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - bin/whatoption
54
+ - lib/whatoption.rb
55
+ homepage: https://github.com/ccmywish/whatoption
56
+ licenses:
57
+ - MIT
58
+ metadata:
59
+ bug_tracker_uri: https://github.com/ccmywish/whatoption/issues
60
+ source_code_uri: https://github.com/ccmywish/whatoption
61
+ post_install_message:
62
+ rdoc_options: []
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ requirements: []
76
+ rubygems_version: 3.3.26
77
+ signing_key:
78
+ specification_version: 4
79
+ summary: 'whatoption: What does the option mean?'
80
+ test_files: []