1

I'm trying to use pdoc to document my python script but I'm having an issue due to the fact that my program requires the use of command line args.

My program is called as follows: myprogram.py -p arg1 -s arg2

And I try to run pdoc as follows: pdoc --html myprogram

But I get an error saying pdoc: error: the following arguments are required: -p

But if I try to run pdoc like such: pdoc --html myprogram.py -p arg1 -s arg2, I get this error:

pdoc: error: unrecognized arguments: -p arg1 -s arg2

IS there a way to run pdoc on a module that requires command line args?

Swoldier
  • 155
  • 5
  • 19

1 Answers1

0

Since pdoc imports the documented modules, you need to nest your CLI program execution under a main guard:

def main():
    from argparse import ArgumentParser
    parser = ArgumentParser(...)
    args = parser.parse_args()
    ...

if __name__ == '__main__':
    # Run main entry point only if the script was run as a program
    # and not if it was merely imported
    main()
K3---rnc
  • 6,717
  • 3
  • 31
  • 46
  • Is there a way to do this without violating PEP8? "Imports are always put at the top of the file, just after any module comments and docstrings, and before module globals and constants." - https://www.python.org/dev/peps/pep-0008/#imports – ProGirlXOXO Jun 28 '21 at 20:29
  • @ProGirlXOXO You could move the line `from argparse import ArgumentParser` to the top of the file? – K3---rnc Jun 29 '21 at 00:04
  • Then pdoc would require you to have argparse installed (and all other 3rd party imports) or else complain. Do I have to choose between these two unattractive options or is there another? – ProGirlXOXO Jun 29 '21 at 01:39
  • @ProGirlXOXO In above particular case, [package _argparse_](https://docs.python.org/3/library/argparse.html) is part of the [standard library](https://en.wikipedia.org/wiki/Standard_library). Other third party imports indeed need to be made available in the environment pdoc is run in. – K3---rnc Jun 29 '21 at 10:55