ITADN

Support user-defined verbs

#1836Openbalki 创建于 2025-07-17
B
balkicommented
miller verbs are great. Easy to use, saves key strokes and easy to understand. It would be nice to extend those benefits to user-defined verbs. ### Proposal Users can create new verbs by registering them using below function. This function will be called in a miller file loaded using `--[m]load`. ``` register_verb(func, name, description, flags) ``` * `name` is string * `description` is string for use in `mlr help` * `flags` is an array of `flag` which is a map with keys `short`, `long`, `description`, `type`, `default`. At least one of `short` or `long` is required. Rest are of them are optional. * `type` can be `"bool"`, `"single"`, or `"multi"`. default is `"single"`. `"multi"` means it can occur multiple times. e.g. `verb -f arg1-f arg2 -f arg3`. Accessed in the function as a list `arg["f"]` will be `["arg1", "arg2", "arg3"]`. * when `type` is `bool`, it can be turned off with `--no-` prefix. i.e. `--no-longname`. * `bool` flags can be coalesced. i.e. `-xyz foo` is same as `-x -y -z foo`. * one of the flag can have `long` set to `[POS]` which refers to positional argument. `type` is always `single` * `func` is a miller function that will be called with one argument `args`. It is a map with passed flags. #### Example ``` register_verb(bconv, "bconv", "Convert bytes to KB, MB, GB or TB", [ { "short": "s", "long":"source", "type":"single", "default":"B", "description":"Source unit. One of B, KB, MB, GB" }, { "short": "t", "long":"target", "type":"single", "default": "auto", "description":"Target unit. One of B, KB, MB, GB, auto. If auto, appropriate unit is chosen" }, { "short": "x", "long":"suffix", "type":"bool", "default":true, "description":"Add suffix after conversion. E.g. 2.4 MB" }, { "long":"[POS]", "description":"comma separated list of fields to convert" } ] ) func bconf(args) { if (args["suffix"]) { ... } } ``` #### Namespace 1. Keep user defined verbs same namespace as standard ones. (similar to user defined functions). 2. Have user-defined verbs start with capital letter. Similar to `go` functions. 3. **[my choice]** Prefix user-defined verbs with `u:` or a different prefix. E.g. `mlr --l2p u:bconv Memory,Swap then head`.
4 条评论