semantic_cli 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 627cd9695ac05760271592c1a5a335bb52000c287bda4bbe320c9ef0ec9dbd84
4
+ data.tar.gz: 1e340edab57285e72a71b86073696df2acf77bbe395920cb7b7c4610c22f4ad0
5
+ SHA512:
6
+ metadata.gz: 29baab18463488236d093243e521ced8732cec1b4d8fd0552e3fdda5eb89622e445b75671bc6fa250e4f33c4ca78fc3bc7ffc43ad488b409ee48d14202aa6641
7
+ data.tar.gz: 637f8f6e0f4bd6fe0d27c77c598c6d65cafbbf1b5b5e5032ccbda2e90be5dc0c30e1dbe4925980d41d344dd90f5bc9d543f7eccd07d59f4d486bc15a65951a67
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2025-10-16
4
+
5
+ - Initial release
@@ -0,0 +1,132 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our
6
+ community a harassment-free experience for everyone, regardless of age, body
7
+ size, visible or invisible disability, ethnicity, sex characteristics, gender
8
+ identity and expression, level of experience, education, socio-economic status,
9
+ nationality, personal appearance, race, caste, color, religion, or sexual
10
+ identity and orientation.
11
+
12
+ We pledge to act and interact in ways that contribute to an open, welcoming,
13
+ diverse, inclusive, and healthy community.
14
+
15
+ ## Our Standards
16
+
17
+ Examples of behavior that contributes to a positive environment for our
18
+ community include:
19
+
20
+ * Demonstrating empathy and kindness toward other people
21
+ * Being respectful of differing opinions, viewpoints, and experiences
22
+ * Giving and gracefully accepting constructive feedback
23
+ * Accepting responsibility and apologizing to those affected by our mistakes,
24
+ and learning from the experience
25
+ * Focusing on what is best not just for us as individuals, but for the overall
26
+ community
27
+
28
+ Examples of unacceptable behavior include:
29
+
30
+ * The use of sexualized language or imagery, and sexual attention or advances of
31
+ any kind
32
+ * Trolling, insulting or derogatory comments, and personal or political attacks
33
+ * Public or private harassment
34
+ * Publishing others' private information, such as a physical or email address,
35
+ without their explicit permission
36
+ * Other conduct which could reasonably be considered inappropriate in a
37
+ professional setting
38
+
39
+ ## Enforcement Responsibilities
40
+
41
+ Community leaders are responsible for clarifying and enforcing our standards of
42
+ acceptable behavior and will take appropriate and fair corrective action in
43
+ response to any behavior that they deem inappropriate, threatening, offensive,
44
+ or harmful.
45
+
46
+ Community leaders have the right and responsibility to remove, edit, or reject
47
+ comments, commits, code, wiki edits, issues, and other contributions that are
48
+ not aligned to this Code of Conduct, and will communicate reasons for moderation
49
+ decisions when appropriate.
50
+
51
+ ## Scope
52
+
53
+ This Code of Conduct applies within all community spaces, and also applies when
54
+ an individual is officially representing the community in public spaces.
55
+ Examples of representing our community include using an official email address,
56
+ posting via an official social media account, or acting as an appointed
57
+ representative at an online or offline event.
58
+
59
+ ## Enforcement
60
+
61
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
62
+ reported to the community leaders responsible for enforcement at
63
+ [INSERT CONTACT METHOD].
64
+ All complaints will be reviewed and investigated promptly and fairly.
65
+
66
+ All community leaders are obligated to respect the privacy and security of the
67
+ reporter of any incident.
68
+
69
+ ## Enforcement Guidelines
70
+
71
+ Community leaders will follow these Community Impact Guidelines in determining
72
+ the consequences for any action they deem in violation of this Code of Conduct:
73
+
74
+ ### 1. Correction
75
+
76
+ **Community Impact**: Use of inappropriate language or other behavior deemed
77
+ unprofessional or unwelcome in the community.
78
+
79
+ **Consequence**: A private, written warning from community leaders, providing
80
+ clarity around the nature of the violation and an explanation of why the
81
+ behavior was inappropriate. A public apology may be requested.
82
+
83
+ ### 2. Warning
84
+
85
+ **Community Impact**: A violation through a single incident or series of
86
+ actions.
87
+
88
+ **Consequence**: A warning with consequences for continued behavior. No
89
+ interaction with the people involved, including unsolicited interaction with
90
+ those enforcing the Code of Conduct, for a specified period of time. This
91
+ includes avoiding interactions in community spaces as well as external channels
92
+ like social media. Violating these terms may lead to a temporary or permanent
93
+ ban.
94
+
95
+ ### 3. Temporary Ban
96
+
97
+ **Community Impact**: A serious violation of community standards, including
98
+ sustained inappropriate behavior.
99
+
100
+ **Consequence**: A temporary ban from any sort of interaction or public
101
+ communication with the community for a specified period of time. No public or
102
+ private interaction with the people involved, including unsolicited interaction
103
+ with those enforcing the Code of Conduct, is allowed during this period.
104
+ Violating these terms may lead to a permanent ban.
105
+
106
+ ### 4. Permanent Ban
107
+
108
+ **Community Impact**: Demonstrating a pattern of violation of community
109
+ standards, including sustained inappropriate behavior, harassment of an
110
+ individual, or aggression toward or disparagement of classes of individuals.
111
+
112
+ **Consequence**: A permanent ban from any sort of public interaction within the
113
+ community.
114
+
115
+ ## Attribution
116
+
117
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
118
+ version 2.1, available at
119
+ [https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
120
+
121
+ Community Impact Guidelines were inspired by
122
+ [Mozilla's code of conduct enforcement ladder][Mozilla CoC].
123
+
124
+ For answers to common questions about this code of conduct, see the FAQ at
125
+ [https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
126
+ [https://www.contributor-covenant.org/translations][translations].
127
+
128
+ [homepage]: https://www.contributor-covenant.org
129
+ [v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
130
+ [Mozilla CoC]: https://github.com/mozilla/diversity
131
+ [FAQ]: https://www.contributor-covenant.org/faq
132
+ [translations]: https://www.contributor-covenant.org/translations
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 raykin
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,135 @@
1
+ # SemanticCli
2
+
3
+ A Ruby DSL for building flag-free CLIs. You define named functions that return shell commands — the framework takes care of parsing, argument resolution, and piping.
4
+
5
+ ```
6
+ watch log nginx grep error
7
+ → Running: tail -f /var/log/nginx/access.log | grep error
8
+ ```
9
+
10
+ ## Installation
11
+
12
+ ```bash
13
+ gem install semantic_cli
14
+ ```
15
+
16
+ ## Usage
17
+
18
+ Create a script (e.g., `restart`):
19
+
20
+ ```ruby
21
+ #!/usr/bin/env ruby
22
+ require 'semantic_cli'
23
+
24
+ fn('nginx') do
25
+ if macos?
26
+ "brew services restart nginx"
27
+ else
28
+ "sudo systemctl restart nginx"
29
+ end
30
+ end
31
+
32
+ fn('psql') do
33
+ if macos?
34
+ "brew services restart postgresql@15"
35
+ else
36
+ "sudo systemctl restart postgresql"
37
+ end
38
+ end
39
+
40
+ fn('') do
41
+ <<~HELP
42
+ Examples:
43
+ restart nginx → restart nginx service
44
+ restart psql → restart postgresql
45
+ HELP
46
+ end
47
+
48
+ run
49
+ ```
50
+
51
+ Then just run: `restart nginx`
52
+
53
+ ### DSL
54
+
55
+ - `fn(name) { |arg| "shell command" }` — define a function that returns a shell command string
56
+ - `cmd(name, command)` — shorthand when no logic is needed
57
+ - `fn('') { "help text" }` — help text, displayed when no arguments are given
58
+ - `macos?` / `linux?` — platform detection helpers
59
+
60
+ ### Argument Styles
61
+
62
+ ```ruby
63
+ fn('log') { |svc| "journalctl -u #{svc} -f" } # required arg: watch log nginx
64
+ fn('http') { |site='google'| "curl #{site}.com" } # optional arg: watch http / watch http github
65
+ fn('dns') { |c1, *rest| "dig #{c1} #{rest.join(' ')}" } # rest args: dns set 8.8.8.8 8.8.4.4
66
+ fn('status') { "kubectl get pods" } # no arg: ops status
67
+ cmd 'deploy', 'kubectl rollout restart deployment' # static string: ops deploy
68
+ ```
69
+
70
+ ### Piping
71
+
72
+ When multiple functions are chained, their outputs are joined with `|`:
73
+
74
+ ```
75
+ watch log nginx grep error
76
+ → Running: journalctl -u nginx -f | grep error
77
+ ```
78
+
79
+ ### Key-Value Syntax
80
+
81
+ Arguments can also be passed with `key:value`:
82
+
83
+ ```
84
+ watch max:5
85
+ → Running: head -n5
86
+ ```
87
+
88
+ ## Use Cases
89
+
90
+ ### Personal Command Palette
91
+
92
+ Wrap the commands you use every day into short, memorable names:
93
+
94
+ ```ruby
95
+ cmd '77', 'ssh root@192.168.77.1'
96
+ cmd '7744', 'ssh root@lzm -p 7744'
97
+ ```
98
+
99
+ ### DevOps Runbook
100
+
101
+ Turn team runbooks into executable scripts instead of wiki pages people copy-paste from:
102
+
103
+ ```ruby
104
+ fn('deploy') { |env| "kubectl rollout restart deployment/app -n #{env}" }
105
+ fn('logs') { |svc| "kubectl logs -f deployment/#{svc} --tail=100" }
106
+ fn('status') { "kubectl get pods -A" }
107
+ ```
108
+
109
+ `ops deploy staging`, `ops logs api`, `ops status` — the team doesn't need to remember the underlying commands.
110
+
111
+ ### Cross-Platform Scripts
112
+
113
+ Write scripts that work on both macOS and Linux:
114
+
115
+ ```ruby
116
+ fn('proxy') do |c1='display'|
117
+ if macos?
118
+ "networksetup -setsocksfirewallproxystate wifi #{c1}"
119
+ else
120
+ "gsettings set org.gnome.system.proxy mode manual"
121
+ end
122
+ end
123
+ ```
124
+
125
+ ## Development
126
+
127
+ ```bash
128
+ bin/setup # install dependencies
129
+ rake test # run tests
130
+ bin/console # interactive prompt
131
+ ```
132
+
133
+ ## License
134
+
135
+ MIT
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "minitest/test_task"
5
+
6
+ Minitest::TestTask.create
7
+
8
+ require "standard/rake"
9
+
10
+ task default: %i[test standard]
@@ -0,0 +1,65 @@
1
+ module SemanticCli
2
+ class DSL
3
+ class Function
4
+ def initialize(block)
5
+ @block = block
6
+ @params = block.parameters
7
+ end
8
+
9
+ def expects_arg?
10
+ @params.any? { |kind, _| kind == :req || kind == :opt }
11
+ end
12
+
13
+ def expects_rest?
14
+ @params.any? { |kind, _| kind == :rest }
15
+ end
16
+
17
+ def call(*args)
18
+ args = args.compact
19
+ if expects_rest?
20
+ @block.call(*args)
21
+ elsif @params.any? { |kind, _| kind == :req }
22
+ @block.call(args.first)
23
+ elsif @params.any? { |kind, _| kind == :opt }
24
+ args.empty? ? @block.call : @block.call(args.first)
25
+ else
26
+ @block.call
27
+ end
28
+ end
29
+ end
30
+
31
+ def initialize
32
+ @functions = {}
33
+ end
34
+
35
+ def define(name, &block)
36
+ name = name.to_s.strip
37
+ if name != "" && name.split.size != 1
38
+ raise ArgumentError, "Function name must be single word or empty string"
39
+ end
40
+ @functions[name] = Function.new(block)
41
+ end
42
+
43
+ def exists?(name)
44
+ @functions.key?(name)
45
+ end
46
+
47
+ def expects_arg?(name)
48
+ fn = @functions[name]
49
+ return false unless fn
50
+ fn.expects_arg?
51
+ end
52
+
53
+ def expects_rest?(name)
54
+ fn = @functions[name]
55
+ return false unless fn
56
+ fn.expects_rest?
57
+ end
58
+
59
+ def call(name, *args)
60
+ fn = @functions[name]
61
+ return unless fn
62
+ fn.call(*args)
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,3 @@
1
+ module SemanticCli
2
+ Fragment = Data.define(:name, :arg, :shell)
3
+ end
@@ -0,0 +1,21 @@
1
+ module SemanticCli
2
+ class Option
3
+ attr_reader :raw, :kind, :name, :arg
4
+
5
+ def initialize(raw)
6
+ @raw = raw
7
+ classify
8
+ end
9
+
10
+ def classify
11
+ if raw.include?(":")
12
+ key, val = raw.split(":", 2)
13
+ @kind, @name, @arg = :kv_function, key, val
14
+ elsif raw =~ /^\d+$/
15
+ @kind, @name, @arg = :parameter, nil, raw
16
+ else
17
+ @kind, @name, @arg = :word, raw, nil
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,94 @@
1
+ module SemanticCli
2
+ class Parser
3
+ ParseResult = Data.define(:fragment, :consumed)
4
+
5
+ def initialize(argv, dsl)
6
+ @tokens = argv.dup
7
+ @dsl = dsl
8
+ @fragments = []
9
+ end
10
+
11
+ def build_fragments
12
+ i = 0
13
+ last_fn = nil
14
+ last_fn_consumed_arg = false
15
+
16
+ while i < @tokens.size
17
+ result = parse_token(i, last_fn, last_fn_consumed_arg)
18
+
19
+ if result
20
+ @fragments << result.fragment
21
+ last_fn = result.fragment.name
22
+ last_fn_consumed_arg = result.consumed > 1
23
+ i += result.consumed
24
+ else
25
+ i += 1
26
+ end
27
+ end
28
+
29
+ @fragments
30
+ end
31
+
32
+ private
33
+
34
+ def parse_token(index, last_fn, last_fn_consumed_arg)
35
+ token = @tokens[index]
36
+ next_token = @tokens[index + 1]
37
+ opt = Option.new(token)
38
+
39
+ case opt.kind
40
+ when :kv_function
41
+ return result(opt.name, opt.arg, 1) if @dsl.exists?(opt.name)
42
+ warn "Unknown function: #{opt.name}"
43
+ nil
44
+
45
+ when :parameter
46
+ if last_fn && @dsl.expects_arg?(last_fn) && !last_fn_consumed_arg
47
+ result(last_fn, opt.arg, 1)
48
+ else
49
+ warn "Orphan parameter: #{opt.arg}"
50
+ nil
51
+ end
52
+
53
+ when :word
54
+ if @dsl.exists?(opt.name)
55
+ if @dsl.expects_arg?(opt.name) && next_token && !@dsl.exists?(next_token) && !next_token.include?(":")
56
+ if @dsl.expects_rest?(opt.name)
57
+ rest_args = collect_rest(index + 1)
58
+ result_rest(opt.name, rest_args, 1 + rest_args.size)
59
+ else
60
+ result(opt.name, next_token, 2)
61
+ end
62
+ else
63
+ result(opt.name, nil, 1)
64
+ end
65
+ elsif last_fn && @dsl.expects_arg?(last_fn) && !last_fn_consumed_arg
66
+ result(last_fn, opt.name, 1)
67
+ else
68
+ warn "Unknown token: #{opt.name}"
69
+ nil
70
+ end
71
+ end
72
+ end
73
+
74
+ def collect_rest(start)
75
+ args = []
76
+ i = start
77
+ while i < @tokens.size
78
+ t = @tokens[i]
79
+ break if @dsl.exists?(t) || t.include?(":")
80
+ args << t
81
+ i += 1
82
+ end
83
+ args
84
+ end
85
+
86
+ def result(name, arg, consumed)
87
+ ParseResult.new(Fragment.new(name, arg, @dsl.call(name, arg)), consumed)
88
+ end
89
+
90
+ def result_rest(name, args, consumed)
91
+ ParseResult.new(Fragment.new(name, args.first, @dsl.call(name, *args)), consumed)
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,31 @@
1
+ module SemanticCli
2
+ class Runner
3
+ def initialize(dsl)
4
+ @dsl = dsl
5
+ end
6
+
7
+ def execute(argv)
8
+ if argv.empty?
9
+ if @dsl.exists?('')
10
+ help_text = @dsl.call('')
11
+ puts help_text unless help_text.to_s.strip.empty?
12
+ else
13
+ warn "No command given."
14
+ end
15
+ return
16
+ end
17
+
18
+ parser = Parser.new(argv, @dsl)
19
+ fragments = parser.build_fragments
20
+
21
+ command = fragments.map(&:shell).compact.join(" | ")
22
+ if command.empty?
23
+ warn "No command built."
24
+ return
25
+ end
26
+
27
+ puts "→ Running: #{command}"
28
+ system command
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SemanticCli
4
+ VERSION = "0.3.0"
5
+ end
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "semantic_cli/version"
4
+ require_relative "semantic_cli/dsl"
5
+ require_relative "semantic_cli/option"
6
+ require_relative "semantic_cli/fragment"
7
+ require_relative "semantic_cli/parser"
8
+ require_relative "semantic_cli/runner"
9
+
10
+ module SemanticCli
11
+ class Error < StandardError; end
12
+
13
+ class << self
14
+ def dsl
15
+ @dsl ||= DSL.new
16
+ end
17
+
18
+ def runner
19
+ @runner ||= Runner.new(dsl)
20
+ end
21
+
22
+ def reset!
23
+ @dsl = nil
24
+ @runner = nil
25
+ end
26
+ end
27
+
28
+ module DSLHelpers
29
+ def fn(name, &block)
30
+ SemanticCli.dsl.define(name, &block)
31
+ end
32
+
33
+ def cmd(name, command)
34
+ SemanticCli.dsl.define(name) { command }
35
+ end
36
+
37
+ def run
38
+ SemanticCli.runner.execute(ARGV)
39
+ end
40
+
41
+ def macos?
42
+ RUBY_PLATFORM.include?("darwin")
43
+ end
44
+
45
+ def linux?
46
+ RUBY_PLATFORM.include?("linux")
47
+ end
48
+ end
49
+ end
50
+
51
+ # Auto-include DSL into top-level context
52
+ include SemanticCli::DSLHelpers
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: semantic_cli
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.0
5
+ platform: ruby
6
+ authors:
7
+ - raykin
8
+ bindir: exe
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies: []
12
+ description: Define named functions that return shell commands, with automatic argument
13
+ resolution and piping
14
+ email:
15
+ - raykincoldxiao@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - CHANGELOG.md
21
+ - CODE_OF_CONDUCT.md
22
+ - LICENSE.txt
23
+ - README.md
24
+ - Rakefile
25
+ - lib/semantic_cli.rb
26
+ - lib/semantic_cli/dsl.rb
27
+ - lib/semantic_cli/fragment.rb
28
+ - lib/semantic_cli/option.rb
29
+ - lib/semantic_cli/parser.rb
30
+ - lib/semantic_cli/runner.rb
31
+ - lib/semantic_cli/version.rb
32
+ homepage: https://github.com/raykin/semantic-cli
33
+ licenses:
34
+ - MIT
35
+ metadata:
36
+ allowed_push_host: https://rubygems.org
37
+ homepage_uri: https://github.com/raykin/semantic-cli
38
+ source_code_uri: https://github.com/raykin/semantic-cli
39
+ changelog_uri: https://github.com/raykin/semantic-cli/releases
40
+ rdoc_options: []
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 3.3.0
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ requirements: []
54
+ rubygems_version: 3.7.1
55
+ specification_version: 4
56
+ summary: A Ruby DSL for building flag-free CLIs
57
+ test_files: []