October 5th, 2024

Pantheon: Parsing command line arguments

The "sheshat" library in Rust enables command line argument parsing without the standard library, supporting various argument types and simplifying structure definitions through macros while ensuring effective error handling.

Read original articleLink Icon
Pantheon: Parsing command line arguments

The article discusses the development of a Rust library named "sheshat" for parsing command line arguments, designed to be used in environments where the standard library may not be available, such as kernel development. The library aims to handle various types of command line arguments, including short flags, long options, and positional arguments. It will provide a derive macro to simplify the parsing process by allowing users to define argument structures easily. The parsing mechanism involves lexing the input arguments and categorizing them into flags, options, and positional arguments. The library will also implement a procedural macro to facilitate the parsing of structs, allowing for attributes that define how fields correspond to command line arguments. The article outlines the challenges of implementing this macro without external dependencies and discusses the need for careful parsing of Rust syntax. It emphasizes the importance of handling errors gracefully and ensuring that the library can effectively parse and validate command line inputs.

- The "sheshat" library is designed for parsing command line arguments in Rust without relying on the standard library.

- It supports various argument types, including short flags, long options, and positional arguments.

- A derive macro will simplify the process of defining argument structures for users.

- The library aims to handle errors effectively and provide clear feedback during parsing.

- The implementation will focus on procedural macros to facilitate argument parsing while managing Rust syntax intricacies.

Link Icon 1 comments
By @IgorPartola - 7 months
Ages ago I was writing a lot of daemons in Python. They all needed to be well behaved UNIX processes: they needed to be able to background themselves, write a PID file atomically and not start a second copy of a PID file for a live process existed, etc.

The main thing I wanted them to do is to behave well with configuration both from a config file and from command line arguments. Lots of libraries exist for either but none existed for both: I wanted to have something that would be able to correctly pull something like the PID file location from a config file but also be able to be overwritten by a command line argument. I also wanted to be as simple to use as possible. The code is hosted at https://github.com/ipartola/groper

I am not sure if in today’s world of systemd this is useful at all. The code probably needs clean up since it was written to be compatible with both Python 2 and 3 at the time. But I have to say that I haven’t seen anything like this since I wrote this code. If there is interest I can take a look at modernizing this.

Also in general I appreciate programs, especially daemons that are “well behaved”: a sensible config file format, sensible arguments parsing, sane defaults, does not background itself without an explicit configuration to do so, logs to stdout and stderr by default. I feel like there is less and less emphasis on stuff like this lately.