How to pass in command line arguments when using ideone?

前端 未结 3 512
轻奢々
轻奢々 2021-02-13 07:25

I\'m using the ideone online interpreter (http://ideone.com/) to test some C++ and Python programs. How do I specify the command line arguments instead of using the STDIN input?

相关标签:
3条回答
  • 2021-02-13 07:58

    Looks like you can't, but a quick hack should do the trick:

    static char * const ARGV[] = { "myprog", "hello", "world", NULL };
    
    int main(int argc, char * argv[])
    {
        argc = 3;
        argv = ARGV;
    
        // ...
    }
    

    Or convert the standard input into args:

    #include <vector>
    #include <string>
    #include <iterator>
    #include <iostream>
    
    std::vector<char *> fabricate(std::vector<std::string> & v)
    {
        std::vector<char *> res(v.size() + 1, NULL);
        for (std::size_t i = 0; i != v.size(); ++i) { res[i] = &v[i][0]; }
        return res;
    }
    
    std::vector<std::string> args_vector((std::istream_iterator<std::string>(std::cin)), std::istream_iterator<std::string>());
    
    std::vector<char *> argv_vector = fabricate(args_vector);
    
    
    int main(int argc, char * argv[])
    {
        argc = args_vector.size();
        argv = argv_vector.data();
    
        // ...
    }
    
    0 讨论(0)
  • 2021-02-13 07:58

    In python you can hardcode like this:

    import sys
    
    print sys.argv
    sys.argv[1:] = ["test1", "test2"]
    print sys.argv
    

    This will output:

    ['prog.py']
    ['prog.py', 'test1', 'test2']
    

    To read from stdin:

    import sys
    import shlex
    
    print sys.argv
    sys.argv[1:] = shlex.split(None)
    print sys.argv
    
    0 讨论(0)
  • 2021-02-13 08:04

    Just initialize to simulate the command line arguments

    static char *argv[] = {"program_name", "1st argument", "2nd argument", NULL};
    static int argc = 3;
    int main() {
    
    }
    
    0 讨论(0)
提交回复
热议问题