Closed gkeep closed 2 years ago
From what I can tell, getopts
requires all available flags to be written down in a case
statement. Feel free to correct me, though.
I found another way to parse flags with case "$line" in --*)
, which functions correctly.
I added two test scripts which compare old and new methods of parsing the flags in the tests
folder.
% ./user_flags_old.sh
loc: /home/gkeep/.config/chromium-flags.conf
flags: --enable-features=VaapiVideoDecoder
# this
--ignore-gpu-blocklist
--enable-gpu-rasterization
--enable-zero-copy
# is
--force-dark-mode
--enable-features=WebUIDarkMode
# a comment
% ./user_flags_new.sh
loc: /home/gkeep/.config/chromium-flags.conf
flags: --enable-features=VaapiVideoDecoder --ignore-gpu-blocklist --enable-gpu-rasterization --enable-zero-copy --force-dark-mode --enable-features=WebUIDarkMode
I looked around a bit and it seems you are right, this is the best we can do without making things too complicated. I did some tests myself and it can handle extra spaces and tabs pretty well, the only thing it cannot handle is comments after a flag (in the same line), but I will leave that for now.
Thanks!
I think this is not the best way to handle this problem if the conf file are meant to be more robust than a flag each line. From the Arch launcher they are using
g_shell_parse_argv()
to parse the input as shell arguments, and the equivalent in bash shall begetopts
AFAIK.Also it will be better if you can add a test file/function, but I can add that later.