Comments (4)
Hi @fuzzypixelz 🙂 nice to hear from you again!
It's a bit of a shame because there's no good place where this feature is documented currently, but we've implemented since 1.3 (I believe) something call the CLI customize mode. The idea is exactly what you describe: be able to pass arguments to Nickel configurations from the command-line.
As Nickel focuses on the merging model to write reusable configuration modules, it's based on record, and not function, but can achieve the same.
If you take any configuration, you can enter the customize mode by adding a --
after the subcommand. For example:
$ nickel eval examples/config-gcc/config-gcc.ncl -- help
[..print help..]
The idea is that this mode turns your configuration into a CLI, so to speak. You can then pass arguments using Nickel syntax (but don't forget to escape shell characters) like foo.bar=1
or 'input.str="Hello"'
. A basic example that would mimic your function idea would be:
# file: config.ncl
let Output = Dyn in
let Contract = Dyn in
{
input.json_data | String,
output | Output =
let result | Contract = std.deserialize 'Json input.json_data in
std.deep_seq result 'Ok,
}
$ nickel eval config.ncl --field output -- 'input.json_data="{\"foo\": 1}"'
'Ok
So I think this should cover your use-case.
from nickel.
Thanks for the response! 😄
Needless to say, I didn't know about the customize mode. The CLI still shows help if I use it incorrectly, but I don't think the option is discoverable IIRC or I must've misread the CLI usage info (to be checked) it is mentioned when executing nickel eval --help
, I simply missed it.
In any case, I think it's an imperfect solution because the caller needs to escape Nickel escape codes by hand when passing strings. The logic behind string escape codes is the responsibility of the interpreter.
Still, this is unfair to the interface because it's supposed to accept Nickel expressions and not strings. Would you be interested in adding a syntax to pass in raw strings? Or perhaps simply add raw strings to the language?
from nickel.
In any case, I think it's an imperfect solution because the caller needs to escape Nickel escape codes by hand when passing strings. The logic behind string escape codes is the responsibility of the interpreter.
I think there are two different aspects here. The fact that we need to escape "
inside a string sounds unrelated to the CLI interface. We have to do that in Nickel source files as well. Typically, you couldn't copy-paste the JSON content directly in a String inside a .ncl
file without escaping either.
As you say, what the customize mode accepts is just normal Nickel expressions, and IMHO that makes sense, because I think you want to distinguish between 2
and "2"
.
One possible nice addition would be to use the special value -
to mean "take from stdin", and this could be considered a String
by default. Or we could have a special syntax to do so, like -String-
or whatever. This doesn't cover all use-cases but is probably a nice addition when you want to forward a generated string to a Nickel configuration.
A second point is the fact that you need to also escape shell special characters. Most of the time you can get by, by just slapping a pair of single quotes around the whole affectation on the CLI, as I did in my example. I honestly don't see how to avoid that anyway, as your input method is the terminal.
Still, this is unfair to the interface because it's supposed to accept Nickel expressions and not strings. Would you be interested in adding a syntax to pass in raw strings? Or perhaps simply add raw strings to the language?
Adding raw strings to the language would make a lot of sense, yeah. We already have the machinery for special strings xxx%" "%
, so it would just amount to have a r
prefix I suppose, like r%"..."%
.
Shall we close this issue, and open other ones if needed (raw strings or the -
argument)? I think the the original issue of passing arguments is possible in current Nickel, but could be improved by those tangential additions.
from nickel.
Yes I think we can close this now. I'm not very sure about the -
argument since it is less useful for multiple arguments. And it parts with the idea of "put any Nickel expression here", so it's a more consequential change. I'll open an issue for raw strings. If you also want the -
argument let's do that too.
from nickel.
Related Issues (20)
- Strange error in std.array.any HOT 2
- Provide contract interface to pyckel HOT 1
- Add functions to create and generically unwrap enum variants HOT 1
- Inconsistent behaviour while loading multiple files vs single file HOT 2
- Getting `record_insert: tried to extend but already exists` when using patterns with duplicate bound variables HOT 2
- Memory leak with long-running nickel process HOT 1
- Opaque foreign values HOT 1
- Update documentation for new pattern features
- Accessing late-bind field fails HOT 6
- Context-sensitive completion in match cases
- Go-lang wrapper for nickel HOT 1
- List equality in contracts can cause some big slowdowns when applied several times HOT 6
- A way to assign a variable to a field with the same name HOT 1
- LSP: Completion shouldn't suggest variable names for record fields
- A completion candidate can shadow a more informative one HOT 3
- Unresolved import causes `nls` to panic HOT 2
- Auto-generate Nickel contracts from Kubernete resource specifications HOT 1
- Non-deterministic typecheck LSP errors when modifying a transitive import of a file HOT 1
- LSP crash on _some_ modifications of a file imported in another HOT 1
- `std.record.get_or` is not working as expected HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nickel.