Skip to content

Better argument parsing - #147

Open
ixs wants to merge 1 commit into
ekalinin:masterfrom
ixs:patch-1
Open

ixs wants to merge 1 commit into
ekalinin:masterfrom
ixs:patch-1

Conversation

@ixs

@ixs ixs commented Nov 15, 2023

Copy link
Copy Markdown

Currently gh-md-toc will only do a limited job parsing arguments.

e.g. gh-md-toc --insert --hide-footer --skip-header README.md would fail as only the --insert option would be parsed as an option and the --hide-footer option would be considered the filename.

This PR adds a new parser in a loop that will parse all arguments instead.

The parser is inspired by https://stackoverflow.com/a/14203146, which solves the problem nicely.

Currently `gh-md-toc` will only do a limited job parsing arguments.

e.g. `gh-md-toc --insert --hide-footer --skip-header README.md` would fail as only the `--insert` option would be parsed as an option and the `--hide-footer` option would be considered the filename.

This PR adds a new parser in a loop that will parse all arguments instead.

The parser is inspired by https://stackoverflow.com/a/14203146, which solves the problem nicely.

@ekalinin ekalinin left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR. A few things need to be fixed before it can be merged:

  1. Conflicts with master. The PR is based on 0.9.0, and master has since added --depth and --numbered. The new loop has no cases for them, so they fall into the -* branch and print help. The --numbered checks (type, and --indent 3 or more for list) also need to run after the loop. Please rebase and add both options. The tests for #25 and #26 cover them.

  2. --indent as the last argument hangs. gh-md-toc README.md --indent never exits: shift 2 fails without shifting, so the loop spins forever. Please check that the value is present.

  3. Unknown options print help and exit 0. gh-md-toc --insert --indnet 2 README.md exits 0, so a typo in CI passes silently while the TOC is never updated. It should print an error and exit non-zero.

  4. - is handled in the middle of parsing. The -) branch returns right away, so options after - are ignored, files before - are dropped, and --insert / --skip-header before - do nothing. - should be collected as a source like any file and processed after the loop.

  5. make lint fails. The rewritten block brings back warnings fixed on master in 1c9e38a (SC2166, SC2006/SC2046, SC2155, SC2086, SC2162) and adds SC2317 and SC2221/SC2222. Keeping master's version of the stdin code after the rebase fixes most of them.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants