Skip to content

Add CLI option to output computation as structured format #763

@aurelleb

Description

@aurelleb

Hi there, I'm looking to make numbat available as a calculator backend for https://github.com/vicinaehq/vicinae and I'm hitting a roadblock trying to integrate it.

Since I'm dealing with a C++ codebase I can't use the library directly, so I thought I could just invoke numbat through its CLI every time a computation is required.

I also considered using FFI but honestly that sounds like a lot of work only to integrate a calculator backend.

The problem is, the output is not meant to be programatically parsed, making this a very hard to implement solution. Errors in particular, cannot be easily parsed at all.

So my suggestion would be to implement a flag that would output the result as a structured format, typically JSON, something like this:

numbat -e '10km to m' --color=never --json

with as an output:

{
  "value": "10000",
  "unit": "m"
}

or

{
  "error": "Incompatible units"
}

The output could be a bit more detailed, I don't know.

I think this would make integrating numbat with launchers or even scripts significantly easier.

If you think this has its place upstream then I'm willing to make a PR for this.


On a side note, it would also be great if we could override specific config options when using the CLI with the -e flag.

In order to make CLI usage a viable option for these kind of use cases we would probably need to add a new exchange rate loading mode that caches it on disk instead of fetching them anew every invocation. But I diverge, that should probably be its own issue.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions