How do I pass an argument from a child task to a parent task in Rake
Not sure if you can set or pass a parameter, but you can use instance variables:
task :first_task => :dep do
puts "something else"
puts @dir
end
task :dep do
puts "something"
@dir = "Foo"
end
Or just local variables:
dir = "default_dir"
task :first_task => :dep do
puts "something else"
puts dir
end
task :dep do
puts "something"
dir = "Foo"
end
For some kinds of task, it may make more sense to set environment variables:
task :first_task => :dep do
puts "something else"
puts ENV['dir']
end
task :dep do
puts "something"
ENV['dir'] = "Foo"
end
Rake: How do I forward params to child Rake calls?
Please check this question. If you cannot modify your tasks to add the parameters ( for some reason ), then you can use environment variables, like:
namespace :db do
task :regenesis do
#because of how devestating this command could be, it's going
# to be forced to use the Test Environment
puts "Re-Generating the Database"
ENV["extra_option"] = "--trace"
Rake::Task["db:drop RAILS_ENV=test"].invoke
Rake::Task["db:create RAILS_ENV=test"].invoke
Rake::Task["db:create RAILS_ENV=test"].invoke
Rake::Task["db:bootstrap RAILS_ENV=test"].invoke
end
end
And in your tasks you'd have to look for ENV["extra_option"]
How can I pass named arguments to a Rake task?
You can say things like this:
rake some_task -- --arg=value
And then, inside your task, ARGV
will be
[ 'some_task', '--arg=value' ]
so you could use OptionParser (or some other option parser) to unpack ARGV
just like in any old CLI script; the funny looking --
is necessary to keep rake from trying to parse --arg=like
as a rake switch.
You're probably better off with the standard environment variable approach, it isn't as ugly as all the --
stuff and it is the usual way of passing arguments to rake tasks.
parent send command line arguments to child
Code:
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>
int main(int argc, char *argv[])
{
pid_t pid;
int status;
int comm[2];
char buffer[BUFSIZ];
// set up pipe
if (pipe(comm) < 0) {
printf("pipe error\n");
return -1;
}
// call fork()
if((pid = fork()) <0)
{
printf("fork error %d\n", pid);
return -1;
}
else if (pid == 0) {
// -- running in child process --
int nChars = 0;
close(comm[1]);
//printf("%d \n",BUFSIZ);
// Receive characters from parent process via pipe
// one at a time, and count them.
int n;
while( (n =read(comm[0], buffer, BUFSIZ)) >0)
{
buffer[n] = 0;
int oneChar, i = 0,endflag = 0;
while((oneChar = buffer[i])!=0)
{
// printf("%d\n",oneChar);
if(oneChar!=EOF)
nChars++;
else
{
endflag = 1;
break;
}
i++;
}
//printf("%s\n",buffer);
if(endflag)
break;
}
printf("nChar : %d",nChars);
// Return number of characters counted to parent process.
return nChars;
}
else {
// -- running in parent process --
//int nChars = 0;
close(comm[0]);
// Send characters from command line arguments starting with
// argv[1] one at a time through pipe to child process.
int a,c;
char endl='\n';
for ( a = 1; a < argc; a++) {
for ( c = 0; c < strlen(argv[a]); c++) {
write(comm[1], &argv[a][c], 1);
}
}
printf("write end\n");
int end = EOF;
write(comm[1],&end,4);
waitpid(pid, &status, 0);
printf("child counted %d chars\n", WEXITSTATUS(status));
return 0;
}
}
Related Topics
Ruby Singleton Methods with (Class_Eval, Define_Method) VS (Instance_Eval, Define_Method)
Rails 3: Generate Unique Codes (Coupons)
How to Reinstall a Gem Using Bundler
Xcode - Configure: Error: No Acceptable C Compiler Found in $Path
Setting Ruby Hash .Default to a List
How to Rescue Model Transaction and Show the User an Error
Testing Actionmailer Multipart Emails(Text and HTML Version) with Rspec
Ruby Net::Http::Get and JSON Responses
Namespaced Models in Rails: What's the State of the Union
Ruby Replace String with Captured Regex Pattern
Library Not Loaded: /Usr/Local/Opt/Readline/Lib/Libreadline.6.Dylib (Loaderror)
An Error Occurred While Installing Debugger-Linecache (1.1.1), and Bundler Cannot Continue