Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Section numbers wrong when multiple argparse directives used in single page #43

Open
djhoese opened this issue Jan 17, 2024 · 4 comments · May be fixed by #44
Open

Section numbers wrong when multiple argparse directives used in single page #43

djhoese opened this issue Jan 17, 2024 · 4 comments · May be fixed by #44
Labels
bug Something isn't working

Comments

@djhoese
Copy link

djhoese commented Jan 17, 2024

This is a rewrite of alex-rudakov#127 from the old project, but I've made it into a smaller reproducible example. The summary of the issue is that Named Arguments and Options sections in a single restructuredtext document (or maybe a single sphinx project) that has a :numbered: TOC tree will always use the section number from the last .. argparse:: directive.

mkdir sample_proj
cd sample_proj
touch myproj.py
touch index.rst
touch utils.rst

myproj.py

def get_parser1(parser):
    parser.add_argument("--flag1", default=1, type=int)
    return parser


def get_parser2(parser):
    parser.add_argument("--flag2", default=2, type=int)
    return parser

index.rst

Welcome to myproj's documentation!
==================================

.. toctree::
   :maxdepth: 2
   :caption: Contents:
   :numbered:

   utils

utils.rst

Utility Scripts
===============


Section A
---------

.. argparse::
    :module: myproj
    :func: get_parser1
    :passparser:

Section B
---------

.. argparse::
    :module: myproj
    :func: get_parser2
    :passparser:

Then generate the docs with:

PYTHONPATH=. sphinx-build . build -C -D extensions=sphinxarg.ext

And open built/utils.html in a browser. You'll see something like:

image

Notice how the "Named Arguments" sections have the same section number. They should be 1.1.1 and 1.2.1.

@djhoese
Copy link
Author

djhoese commented Jan 17, 2024

So the primary issue stems from the ID of the section being based on the section title:

https://github.com/ashb/sphinx-argparse/blob/bf1429aa0a4c51ae63d8d6baca0c163a5c6a3a64/sphinxarg/ext.py#L96

But those section titles are always the same:

https://github.com/ashb/sphinx-argparse/blob/bf1429aa0a4c51ae63d8d6baca0c163a5c6a3a64/sphinxarg/parser.py#L159-L168

So I see a couple solutions:

  1. Allow the user to provide an id_prefix. Another option for something so small and low-level seems annoying. Could also be the user can specify the ID in its entirety.
  2. Add some other information, for example from the parent node, to the ID in ext.py to make them depend on the uniqueness of something outside of sphinx-argparse.
  3. Keep global state for sections and add a integer suffix (or other) to the IDs based on the number we've seen. Main problem with this is the complexity and that this extension is defined as allowing parallel reads. I can't imagine doing this correctly would be fun.

@djhoese
Copy link
Author

djhoese commented Jan 17, 2024

Ah option 2 wouldn't work if we're just using the parent node information as you could technically have two .. argparse:: directives in the same parent section.

@tdegeus
Copy link

tdegeus commented Feb 16, 2024

I have a similar request: I would like to not have the headings Positional Arguments and Named Arguments at all (or have them not as headers). These headers give a messy TOC in the sidebar, e.g.:

outline

One could have an option :noheaders:, e.g.:

.. argparse::
    :module: foo
    :func: _bar_parser
    :prog: bar
    :noheaders:

@djhoese
Copy link
Author

djhoese commented Feb 16, 2024

Yeah I thought I about adding something like that in my PR for fixing my need in #44. Or rather letting the user specify each header. Like :posheader: "" and :namedheader: "" would remove the section titles. Actually maybe they should be referred to as titles, not headers.

That said, your request brings up a harder question to answer. At least it makes me scratch my head. What does a section without a title look like? See this code in my PR for what I mean:

https://github.com/ashb/sphinx-argparse/pull/44/files#diff-c3ed14231074b274607bc7a6afc41f6a281fb8b575354e00b41fe667882be50fR96-R98

It's creating a section node then a title for that section. The docutils docs say that a section has a title: https://docutils.sourceforge.io/docs/ref/doctree.html#section. I'm not sure it is possible to have a section without a title. So then to make your request work you'd be talking about no sections at all...I suppose that might work. Might be some ugly if-statements, but could work.

Sorry, just brainstorming.

@ashb ashb added the bug Something isn't working label May 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants