El jueves, 30 de octubre de 2014 20:17:55 UTC-6, Stefan Karpinski escribió: > > You can't really without resorting to a fragile hack – stuff that's not in > double quotes must be valid Julia syntax and the macro will not get it in > raw form but in parsed form. >
Does the following count as a fragile hack? (Probably yes!) macro run(file, args...) args = [file, args...] return esc(:(ARGS = map(string, $args)[2:end]; include(string($args[1])))) end julia> @run test.jl a b c a b c Though you must do julia> @run "/Users/david/test.jl" a b c a b c with quotes, which is what I guess you referred to with "parsed form"? > > On Thu, Oct 30, 2014 at 10:13 PM, David P. Sanders <dpsa...@gmail.com > <javascript:>> wrote: > >> >> >> El jueves, 30 de octubre de 2014 20:01:40 UTC-6, David P. Sanders >> escribió: >> >>> >>> >>> El jueves, 30 de octubre de 2014 09:11:08 UTC-6, Martin Klein escribió: >>>> >>>> >>>> >>>> Am Donnerstag, 30. Oktober 2014 14:42:28 UTC+1 schrieb Daniel Carrera: >>>>> >>>>> How about this macro: >>>>> >>>>> macro run(file, args...) >>>>> return esc(:(ARGS = $args; include($file))) >>>>> end >>>>> >>>>> For example: >>>>> >>>>> --------------------------------------------- >>>>> $ cat ./test.jl >>>>> #!/usr/bin/julia >>>>> >>>>> for a in ARGS >>>>> println(a) >>>>> end >>>>> >>>>> $ julia >>>>> ... >>>>> julia> macro run(file, args...) >>>>> return esc(:(ARGS = $args; include($file))) >>>>> end >>>>> >>>>> julia> @run "test.jl" 1 2 3 4 >>>>> 1 >>>>> 2 >>>>> 3 >>>>> 4 >>>>> --------------------------------------------- >>>>> >>>>> Does that do what you want? >>>>> >>>> >>>> Yes, almost. Thank you for this nice small solution. I still have to >>>> enclose most of the arguments with quotation marks though, which is a >>>> minor >>>> annoyance. I'm quite sure that one could extend the macro to automatically >>>> convert all given arguments to strings. >>>> >>> >>> I think the following should do the trick: >>> >>> macro run(file, args...) >>> return esc(:(ARGS = map(string, $args); include($file))) >>> end >>> >>> >> I can't work out how to remove the quotes from the filename in order to do >> >> @run test.jl a b c >> >> >> >>> David >>> >>> >>> >>>> When I find the time for it I will use this as an excuse to finally try >>>> to learn Julia's metaprogramming capabilities. >>>> >>>> I would also suggest that something like that should be added to Base. >>>> >>>> Thanks! >>>> Martin >>>> >>>> >>>>> >>>>> Cheers, >>>>> Daniel. >>>>> >>>>> On Thursday, 30 October 2014 10:22:59 UTC+1, Martin Klein wrote: >>>>>> >>>>>> Hi, >>>>>> >>>>>> when using Python I usually execute my programs within IPython by >>>>>> using the %run command, which executes a given script with the given >>>>>> arguments, but keeps all of the defined variables in the namespace after >>>>>> execution. Unfortunately, I couldn't find any fully equivalent command >>>>>> for >>>>>> the Julia REPL. I know about include(), but I couldn't figure out how to >>>>>> give command line arguments to the script, which I want to run (i.e. the >>>>>> stuff which is saved in the ARGS array). >>>>>> >>>>>> Additionally, IPython provides the handy %pdb command, which >>>>>> automatically opens the ipython debugger when an exception is thrown >>>>>> (i.e. >>>>>> it's an exception breakpoint). This is also a very nice feature to >>>>>> quickly >>>>>> investigate errors. I know that Julia doesn't have a full-blown debugger >>>>>> yet (besides debug.jl), but nevertheless this would be a nice future >>>>>> feature for the REPL once there is a Julia debugger. >>>>>> >>>>>> Alternatively, I would be interested in your current >>>>>> first-run-and-debug workflow. I like to write my code in vim but ifter >>>>>> execution I want to directly investigate the state of my program using >>>>>> the >>>>>> REPL. >>>>>> >>>>>> Best, >>>>>> Martin >>>>>> >>>>> >