title: Generate json schema from argparse CLI
body: <p>I have a CLI written with <a href="https://docs.python.org/3/library/argparse.html" rel="nofollow noreferrer"><code>argparse</code></a> and I was wondering if there was a way to produce a JSON schema from the <code>ArgumentParser</code>? The thought behind this being to distribute the JSON schema to extensions interfacing with the application, thus removing the need for each extension to write and maintain their own schema.</p>
  <p>My idea was to</p>
  <ol>
  <li>Convert the <code>argparse.ArgumentParser</code> to Python dictionary or JSON file</li>
  <li>and then pass that into a JSON schema generator</li>
  </ol>
  <h2>Example</h2>
  <pre class="lang-py prettyprint-override"><code>import argparse
  from genson import SchemaBuilder
  
  parser = argparse.ArgumentParser(
      description=&quot;Some description&quot;, prog=&quot;myprog&quot;, usage=&quot;myprog [options]&quot;
  )
  parser.add_argument(
      &quot;-v&quot;,
      &quot;--version&quot;,
      action=&quot;store_true&quot;,
      help=&quot;Print server version number and exit&quot;,
  )
  parser.add_argument(
      &quot;-c&quot;,
      &quot;--config&quot;,
      type=str,
      default=&quot;.fortls&quot;,
      help=&quot;Configuration options file (default file name: %(default)s)&quot;,
  )
  args = vars(parser.parse_args(&quot;&quot;))
  # Generate schema
  builder = SchemaBuilder()
  builder.add_schema({&quot;type&quot;: &quot;object&quot;, &quot;properties&quot;: {}})
  for k, v in args.items():
      builder.add_object({k: v})
  print(builder.to_json(indent=2))
  </code></pre>
  <h3>Output</h3>
  <pre class="lang-json prettyprint-override"><code>{
    &quot;$schema&quot;: &quot;http://json-schema.org/schema#&quot;,
    &quot;type&quot;: &quot;object&quot;,
    &quot;properties&quot;: {
      &quot;version&quot;: {
        &quot;type&quot;: &quot;boolean&quot;
      },
      &quot;config&quot;: {
        &quot;type&quot;: &quot;string&quot;
      }
    }
  }
  </code></pre>
  <p>However, I quickly realised that calling <code>vars(parser().parse_args(&quot;&quot;))</code> to convert the CLI into a dictionary resulted into a lot of information being lost, like descriptions and required.</p>
  <p>Is there another way of doing this? I am open to swapping<code>argparse</code> with some other CLI if it would make generating a schema easier.</p>
  <h2>Additional resources</h2>
  <p><a href="https://stackoverflow.com/questions/7341537/tool-to-generate-json-schema-from-json-data">Tool to generate JSON schema from JSON data</a></p>