Compare commits
2 Commits
b94a21cb2e
...
194ab5e1f9
Author | SHA1 | Date |
---|---|---|
|
194ab5e1f9 | |
|
c72e178787 |
|
@ -55,7 +55,7 @@ Where:
|
||||||
### Flags
|
### Flags
|
||||||
|
|
||||||
* `-1`, `--first-only`: Return only the first match
|
* `-1`, `--first-only`: Return only the first match
|
||||||
* `-q`, `--quiet`: Don't print warnings
|
* `-e`, `--errors`: print warnings
|
||||||
* `-t`, `--text`: Print only the [innerText](https://developer.mozilla.org/fr/docs/Web/API/HTMLElement/innerText) of the matched elements
|
* `-t`, `--text`: Print only the [innerText](https://developer.mozilla.org/fr/docs/Web/API/HTMLElement/innerText) of the matched elements
|
||||||
|
|
||||||
## Motivation
|
## Motivation
|
||||||
|
|
18
main.lua
18
main.lua
|
@ -22,7 +22,7 @@ local function print_usage()
|
||||||
logger.print()
|
logger.print()
|
||||||
logger.print(" Flags:")
|
logger.print(" Flags:")
|
||||||
logger.print(" -1, --first-only: return only the first match")
|
logger.print(" -1, --first-only: return only the first match")
|
||||||
logger.print(" -q, --quiet: Don't print warnings")
|
logger.print(" -e, --errors: print warnings")
|
||||||
logger.print(" -t, --text: Print only the innerText of the matched elements")
|
logger.print(" -t, --text: Print only the innerText of the matched elements")
|
||||||
os.exit(1)
|
os.exit(1)
|
||||||
end
|
end
|
||||||
|
@ -54,7 +54,7 @@ local SHORTHAND_FLAGS = {
|
||||||
if #arg < 2 then
|
if #arg < 2 then
|
||||||
logger.printerr("Error: Not enough arguments")
|
logger.printerr("Error: Not enough arguments")
|
||||||
print_usage()
|
print_usage()
|
||||||
return 1
|
os.exit(1)
|
||||||
end
|
end
|
||||||
|
|
||||||
local flags = {}
|
local flags = {}
|
||||||
|
@ -66,7 +66,7 @@ for _, argument in ipairs(arg) do
|
||||||
if not SHORTHAND_FLAGS[letter] then
|
if not SHORTHAND_FLAGS[letter] then
|
||||||
logger.printerr("Unknown flag: -"..letter..".")
|
logger.printerr("Unknown flag: -"..letter..".")
|
||||||
print_usage()
|
print_usage()
|
||||||
return 1
|
os.exit(1)
|
||||||
end
|
end
|
||||||
|
|
||||||
local flag = SHORTHAND_FLAGS[letter]
|
local flag = SHORTHAND_FLAGS[letter]
|
||||||
|
@ -82,7 +82,7 @@ for _, argument in ipairs(arg) do
|
||||||
if not LONGHAND_FLAGS[flagname] then
|
if not LONGHAND_FLAGS[flagname] then
|
||||||
logger.printerr("Unknown flag: --"..flagname..".")
|
logger.printerr("Unknown flag: --"..flagname..".")
|
||||||
print_usage()
|
print_usage()
|
||||||
return 1
|
os.exit(1)
|
||||||
end
|
end
|
||||||
|
|
||||||
local flag = LONGHAND_FLAGS[flagname]
|
local flag = LONGHAND_FLAGS[flagname]
|
||||||
|
@ -106,7 +106,7 @@ end
|
||||||
if #positionals > 2 then
|
if #positionals > 2 then
|
||||||
logger.printerr("Error: too many arguments !")
|
logger.printerr("Error: too many arguments !")
|
||||||
print_usage()
|
print_usage()
|
||||||
return 1
|
os.exit(1)
|
||||||
end
|
end
|
||||||
|
|
||||||
local html_file = positionals[1]
|
local html_file = positionals[1]
|
||||||
|
@ -115,13 +115,13 @@ local html = nil
|
||||||
if html_file ~= "-" then
|
if html_file ~= "-" then
|
||||||
if not( file_exists( html_file )) then
|
if not( file_exists( html_file )) then
|
||||||
logger.printerr("File doesn't exist: " .. html_file)
|
logger.printerr("File doesn't exist: " .. html_file)
|
||||||
return 2
|
os.exit(2)
|
||||||
end
|
end
|
||||||
|
|
||||||
local handle = io.open( html_file, "r" )
|
local handle = io.open( html_file, "r" )
|
||||||
if not handle then
|
if not handle then
|
||||||
logger.printerr("Failed to open file " .. html_file)
|
logger.printerr("Failed to open file " .. html_file)
|
||||||
return 2
|
os.exit(2)
|
||||||
end
|
end
|
||||||
|
|
||||||
html = handle:read("a")
|
html = handle:read("a")
|
||||||
|
@ -217,13 +217,13 @@ if flags[FLAGS.FIRST_ONLY] then
|
||||||
|
|
||||||
if flags[FLAGS.INNER_TEXT] then
|
if flags[FLAGS.INNER_TEXT] then
|
||||||
logger.print( elements[1]:inner_text() )
|
logger.print( elements[1]:inner_text() )
|
||||||
return 0
|
os.exit(0)
|
||||||
end
|
end
|
||||||
|
|
||||||
logger.print( HTML.tostring( elements[1] ) )
|
logger.print( HTML.tostring( elements[1] ) )
|
||||||
end
|
end
|
||||||
|
|
||||||
return 0
|
os.exit(0)
|
||||||
end
|
end
|
||||||
|
|
||||||
for _, el in ipairs(elements) do
|
for _, el in ipairs(elements) do
|
||||||
|
|
Loading…
Reference in New Issue