nicolewhite / cycli

A Command Line Interface for Neo4j's Cypher.
MIT License
271 stars 25 forks source link

Allow parameters to be set with export #34

Closed nicolewhite closed 8 years ago

nicolewhite commented 8 years ago

gif

Parameters can be set with export key=value and viewed with env.

Closes #33.

nicolewhite commented 8 years ago

Hey, good catch with the 0! That's an easy fix.

Can you show me the full query that threw that error?

nicolewhite commented 8 years ago

Hey @jexp what do you think of handling the eval stuff by having a flag -e for starting cycli in "evaluate exports" mode? With the changes above:

$ cycli -e
> export nums=[x**2 for x in range(5)]
> env
nums=[0, 1, 4, 9, 16]
> UNWIND {nums} AS n RETURN n;
   | n 
---+----
 1 |  0
 2 |  1
 3 |  4
 4 |  9
 5 | 16

15 ms

So by default it will parse the value with json.loads but if you're in -e mode it'll parse the value with eval.

jexp commented 8 years ago

Hmm that would mean depending on which mode you run cycli with your scripts would work or not? I think the eval thing is something on a case-by-case basis rather.

I could rather imagine an export -e nums=[x**2 for x in range(5)] if you rather want to have a flag than a second keyword :)

jexp commented 8 years ago

Would it be possible for the eval code to access existing parameters? Like env['nums'] ?

nicolewhite commented 8 years ago

Yeah, that's a good point about scripts. I was trying to avoid having a separate keyword. :) But I like your idea of using a flag for the export command itself.

nicolewhite commented 8 years ago

I think I'm going to stick with the -e flag to start cycli in "evaluate export" mode despite your (@jexp) concern about script compatibility. Because anything you do in the default mode (where it uses json.loads) should be compatible with eval mode. For example, setting constants such as strings or numbers will be the same whether it's passed to json.loads or eval:

>>> json.loads('54')
54
>>> eval('54')
54

>>> json.loads('"Tom Hanks"')
'Tom Hanks'
>>> eval('"Tom Hanks"')
'Tom Hanks'

Same with maps and collections:

>>> json.loads('{"name":"Nicole"}')
{'name': 'Nicole'}
>>> eval('{"name":"Nicole"}')
{'name': 'Nicole'}

>>> json.loads('[1,2,3]')
[1, 2, 3]
>>> eval('[1,2,3]')
[1, 2, 3]

So you can run your scripts in -e mode to ensure compatibility. Can you think of any exceptions to this?

nicolewhite commented 8 years ago

Btw I added the ability to access variables by key.

> export age=54
> env
age=54
> env["age"]
54
> env['age']
54
> env['lol']
>
nicolewhite commented 8 years ago

Alright, new plan is just to use eval all the time since anything that you'd throw at json.loads can be handled by eval as far as Cypher syntax is concerned.

nicolewhite commented 8 years ago

Merged with https://github.com/nicolewhite/cycli/commit/453343643ea0f29b198fe37e5310efd8b974ff57.