/python-argparse-generator

NPM package for generation of Python code (as text) to streamline writing boilerplate argparse setup.

Primary LanguageTypeScriptMIT LicenseMIT

Python Argparse Generator

npm version

Package for generation of Python code (as text) to streamline writing boilerplate Python argparse setup.

Installing

Using npm:

$ npm install python-argparse-generator

Using unpkg CDN:

<script src="https://unpkg.com/python-argparse-generator/lib/index.js"></script>

Example

Within HTML:

<html>
<head>
    <script>var exports={}</script>
    <script src="https://unpkg.com/python-argparse-generator/lib/index.js"></script>
</head>
<script>
    const pythonCode = argparseCode([
        { name: 'folder', type: 'str', variableName: 'folder', default: '"/data"', defaultDisplay: '"/data"' },
        { name: 'limit', type: 'int', variableName: 'limit', default: '10', defaultDisplay: '10' },
    ]);
    console.log(pythonCode);
</script>
</html>

Within Typescript:

import { Argument, argparseCode } from 'python-argparse-generator';

const args: Argument[] = [
    { name: 'folder', type: 'str', variableName: 'folder', default: '"/data"', defaultDisplay: '"/data"' },
    { name: 'limit', type: 'int', variableName: 'limit', default: '10', defaultDisplay: '10' },
];

const pythonCode = argparseCode(args);

Where content of pythonCode string is:

import argparse
from typing import Dict, Any
  
def main(folder: str, limit: int) -> None:
    # Contents of main
    return


def cli() -> Dict[str, Any]:
    formatter_class = argparse.ArgumentDefaultsHelpFormatter
    parser = argparse.ArgumentParser(formatter_class=formatter_class)

    parser.add_argument("folder", type=str, default="/data")
    parser.add_argument("limit", type=int, default=10)

    args = parser.parse_args()

    return {"folder": args.folder,
            "limit": int(args.limit)}

if __name__ == '__main__':
    args = cli()
    main(folder=args["folder"], limit=args["limit"])