Skip to content

Commit 619c34c

Browse files
att
1 parent 5eeb905 commit 619c34c

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

README.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,54 @@ lua teste.lua conf:test a b
8989
if will show:
9090
```txt
9191
test
92+
```
93+
## Compact Flags size
94+
You also can get the compact flags size
95+
```lua
96+
argv = require("luargv")
97+
local conf_flags = { "conf:", "conf=" }
98+
local size = argv.get_compact_flags_size(conf_flags)
99+
for i = 1, size do
100+
local current_conf = argv.get_compact_flags(conf_flags, i)
101+
print("conf " .. i .. ":" .. current_conf)
102+
end
103+
```
104+
if you run:
105+
```shell
106+
lua teste.lua conf=a conf:b
107+
```
108+
the output will be:
109+
```txt
110+
conf 1:b
111+
conf 2:a
112+
```
113+
## Unused flags
114+
with unsed and unused flags, you can make complex CLIS, by combining flags and args
115+
116+
```lua
117+
argv = require("luargv")
118+
--these its required to mark as used
119+
argv.get_arg_by_index(1)
120+
argv.get_arg_by_index(2)
121+
local output = argv.get_flag_arg_by_index({ "out", "o" }, 1)
122+
if not output then
123+
print("output its required")
124+
return
125+
end
126+
local entry = argv.get_next_unused()
127+
if not entry then
128+
print("entry its required")
129+
return
130+
end
131+
local error_flag = argv.get_next_unused()
132+
if error_flag then
133+
print("unused flag", error_flag)
134+
return
135+
end
136+
137+
print("output:", output)
138+
print("entry:", entry)
139+
92140
```
93141

94142
## Configuring the project

src/luargv/compact_flag.lua

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,29 @@ luargv.get_compact_flags = function(flags, index, default)
3131

3232
return default
3333
end
34+
35+
---@class Argv
36+
---@field get_compact_flags_size fun(flags:string[]|string):number
37+
luargv.get_compact_flags_size = function(flags)
38+
local converted_flags = flags
39+
if luargv.type(flags) == "string" then
40+
---@type string
41+
flags = flags
42+
converted_flags = { flags }
43+
end
44+
local total = 0
45+
for i = 1, #flags do
46+
local flag_name = flags[i]
47+
local flag_size = luargv.get_str_size(flag_name)
48+
local args_size = luargv.get_total_args_size()
49+
for i = 1, args_size do
50+
local current = luargv.get_arg_by_index_not_adding_to_used(i)
51+
52+
if private_luargv.starts_with(current, flag_name) then
53+
total = total + 1
54+
end
55+
end
56+
end
57+
58+
return total
59+
end

0 commit comments

Comments
 (0)