Skip to main content

struct2args

This is a simple Python tool to convert structured data such as yaml,toml, or json to command line arguments. It is intended to allow for easy configuration of command line tools.

Installation

Homebrew coming eventually. For now you can install it with pip:

pip install struct2args

or Pipx:

pipx install struct2args

Usage

Usage is pretty straightforward. It reads a structured data file and outputs a list of command line arguments as a string. The file should have the following format:

__args__: # This is required. Each item in the list is a set of arguments.
  - _name: 'nginx-test' # The name to search for 
    name: 'nginx-test-{% gitcommit %}'
    p: '80:{% get_free_port 80 %}'
    _pos: 'nginx'
  - _name: 'nginx-detached'
    name: 'nginx-detached-{% gitcommit %}'
    p: '80:80'
    _pos:
      - '-d'
      - 'nginx'

Anything that starts with an underscore is a special command and won't be converted to an argument. The following special commands are supported:

__args__: Required in all files. This should be a list of arguments _pos*: Anything starting with _pos will be converted to a positional argument. If it is a list, each item will be interpreted as a separate positional argument.

Otherwise, all other keys will be converted to command line arguments. The key name will be the argument name and the value. So in the example above: name will be converted to --name nginx-test-{% gitcommit %}.

To use the tool you can run it two ways:

1. Command line to xargs

```bash
struct2args <file> | xargs <command>
```

2. Passing in a --cmd argument

```bash
struct2args --cmd <command> <file>
```

Jinja2 Templating

The tool uses Jinja2 templating to allow for dynamic arguments. The standard functions are available as well as the ones from jinja2-git.

In addition, there is a custom tag of get_free_port. This will return a free port on the host machine starting at the port number specified. For example, {% get_free_port 80 %} will return a free port starting at 80. This is useful for things like port forwarding. You can declare a maximum port number by passing in an optional second argument. For example {% get_free_port 80 90 %} will return a free port between 80 and 90.

Metadata

Release files for struct2args 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for struct2args 0.1.1
File Size Uploaded
struct2args-0.1.1.tar.gz 3.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for struct2args 0.1.1
File Interpreter ABI Platform
struct2args-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 8.6 kB

Release files / struct2args-0.1.1.tar.gz

Download URL struct2args-0.1.1.tar.gz
Size 3.8 kB
Tags Source
SHA-256 checksum
How to use checksums
c47363e77e91fef1b874594d6d3c90d50caf729ac6af83057338bd35e71bd81e
BLAKE2b-256 checksum
How to use checksums
941f72804ef62d91a17f21ddc997b2bb21335076b46dacf3057e6a579e2b1913
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.1 CPython/3.10.6 Darwin/22.2.0

Release files / struct2args-0.1.1-py3-none-any.whl

Download URL struct2args-0.1.1-py3-none-any.whl
Size 4.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b0209f32b29bb6ff24de91ec0554d90c983929b754a670ae4a0f084ea77c306f
BLAKE2b-256 checksum
How to use checksums
126df889ed2b80cd7e44b7bde204360e2305313c31019133b89f3b2d9a64a26d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.1 CPython/3.10.6 Darwin/22.2.0

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page