Re: finding locations of many files via kpse

Erik Nijenhuis <[email protected]>
Newsgroups gmane.comp.tex.texhax
Message-ID <[email protected]>
Hi Karl,

On Mon, 2024-08-05 at 14:18 -0600, Karl Berry wrote:
> Specifically, I used getopt_long_only pervasively throughout the TeX
> engines, drivers, etc., back when I unified option handling among the
> programs extant in the late 1980s. Programs added since then sometimes
> use getopt_long_only, sometimes not. We use the Perl equivalent of
> getopt_long_only in tlmgr and install-tl.
Thanks, that clarifies a lot!

> I don't know what you mean. lua-alt-getopt was an early Lua option
> parsing package written independently by Aleksey Cheusov (and not
> updated since 2010). It's not used within TL as far as I know.
> Grepping in a bindir, I see that luaotfload-tool uses it, and, so far as
> that grep showed, nothing else.
Because it would be nice if there's a utility which makes it easy and consistent
for anyone who would want to create a CLI in LuaTeX. However, the script I
mentioned doesn't support `getopt_long_only` after some investigation
unfortunately...

> Me too. That's why I used it. At some point I had to copy the source
> files into TL (kpathsea/getopt*) because of incompatibilities, but it's
> still GNU getopt.
> 
>     However, from a historical standpoint, the alternative getopt
>     library within TeX Live should be favored instead,
> 
> I'm not aware of any "alternative library" in TL (for C).
Thanks for clarifying. With alternative library I meant the getopt files in TL.
Didn't know it's an exact copy of GNU getopt.

So to follow up on Norbert's synopsis, it could be something like:
```lua
local function parse_opts(args)
  local options = {
    mark_sys_files = false,
    progname = "lualatex"
  }
  local remaining_args = {}
  local i = 1
  while i <= #args do
    local arg = args[i]
    if arg:match("^%-%-?mark%-sys%-files$") then
      options.mark_sys_files = true
    elseif arg:match("^%-?%-?progname=") then
      options.progname = arg:match("^%-?%-?progname=(.*)")
    elseif arg:match("^%-?%-?progname$") then
      i = i + 1
      if i <= #args then
        options.progname = args[i]
      else
        error("Option 'progname' requires an argument")
      end
    else
      table.insert(remaining_args, arg)
    end
    i = i + 1
  end
  return options, remaining_args
end

local function parse_args(args)
  local options, remaining_args = parse_opts(args)

  if #remaining_args == 0 then
    error("Expected subdir argument")
  end

  local subdir = remaining_args[1]

  return options, subdir
end

local options, subdir = parse_args(arg)

if options.mark_sys_files then
  print("System files will be marked.")
else
  print("System files will not be marked.")
end

print("Program name:", options.progname)
print("Subdirectory:", subdir)
```

This would then work for the following cases:
```bash
texlua norbert-search.lua --mark-sys-files --progname=myprogram subdir
texlua norbert-search.lua --mark-sys-files --progname myprogram subdir
texlua norbert-search.lua -mark-sys-files -progname=myprogram subdir
texlua norbert-search.lua -mark-sys-files -progname myprogram subdir
```
Although, subdir might be an option as well and by default the cwd.


Best, Erik
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.