I have code like the following
flags->add_option("--name", name_, "The name.")->required();
I want to be able to pass strings like "aa$p$aa" to the --name parameter. However, this does not seem to work with CLI11, and the name gets truncated to just "aa". I need to escape the $ characters to properly read the strings. So aa\$p\$aa works fine, and I get the expected string ("aa$p$aa").
Changing the function to add_flag() does not work either.
Is there a way to be able to pass arbitrary strings to a parameter as with either function calls above?
Your operating system takes the command you type in and executes the given program, passing the parameters to it.
The interpolation, and the handling of $ characters in typed-in commands is handled by your operating system as part of executing the typed-in command. Your compiled C++ program receives the transformed arguments, as parameters. Your C++ program has no means to control what has happened before it was even executed.
Related
In my code the following line gives me data that performs the task its meant for:
const char *key = "\xf1`\xf8\a\\\x9cT\x82z\x18\x5\xb9\xbc\x80\xca\x15";
The problem is that it gets converted at compile time according to rules that I don't fully understand. How does "\x" work in a String?
What I'd like to do is to get the same result but from a string exactly like that fed in at run time. I have tried a lot of things and looked for answers but none that match closely enough for me to be able to apply.
I understand that \x denotes a hex number. But I don't know in which form that gets 'baked out' by the compiler (gcc).
What does that ` translate into?
Does the "\a" do something similar to "\x"?
This is indeed provided by the compiler, but this part is not member of the standard library. That means that you are left with 3 ways:
dynamically write a C++ source file containing the string, and writing it on its standard output. Compile it and (providing popen is available) execute it from your main program and read its input. Pretty ugly isn't it...
use the source of an existing compiler, or directly its internal libraries. Clang is probably a good starting point because it has been designed to be modular. But it could require a good amount of work to find where that damned specific point is coded and how to use that...
just mimic what the compiler does, and write your own parser by hand. It is not that hard, and will learn you why tests are useful...
If it was not clear until here, I strongly urge you to use the third way ;-)
If you want to translate "escape" codes in strings that you get as input at run-time then you need to do it yourself, explicitly.
One way is to read the input into one string. Then copy the characters from that source string into a new destination string, one by one. If you see a backslash then you discard it, fetch the next character, and if it's an x you can use e.g. std::stoi to convert the next few characters into its corresponding integer value, and append that number to the destination string (either adding it with std::to_string, or using output string streams and the normal "output" operator <<).
My understanding is that the function char *tparm(char *str, ...); just converts the given string str to an expanded parameterized version which will be fine to use with stdout outputting functions like printf or cout. But the man page mentions -
Parameterized strings should be passed through tparm to instantiate them. All terminfo strings [including the output of tparm] should be printed with tputs or putp.
So can I parse terminfo entries and use tparm() on them passing appropriate parameters and output them using stdout output functions? Also I'm doing the checks of non-tty output and ignoring these methods so I got that base covered.
Sure, you can. But some capability strings include padding and time delays, which tparm assumes will be interpreted by tputs.
For instance, the flash capability would use time-delays, which are passed along to tputs (using the syntax described in the terminfo(5) manual page).
I'm trying to get value from .cc. In my file ,
AgentCPU.h and AgentCPU.cc
there is a integer called "npkts_" and a function "recv" for receive Packets
when I finally finished a packet , I will increase the "npkts_" 1
In further , in my tcl script I want to access the "npkts_"
my code is like this
set cpu [new Agent\AgentCPU]
puts [format "%d" "$cpu set npkts_"]
However , ther value is not correct
it is same to the value when I construct my AgentCPU like this
AgentCPU::AgentCPU(): Agent(PT_TASK)
{
...
npkts_=199;
...}
the value will be 199,
and in the "recv" function , I use "printf" to check if there is any problem
...
npkts_++;
printf("npkts is %d\n",npkts);
...
and the value here is correct,every time I receive Packet will increase the "npkts"
Is there any code wrong??
On the other hand, I use another way to debug
In my "recv" function
...
npkts_++;
Tcl& tcl = Tcl:;instance();
tcl.evalf("puts \" npkts is %d""",npkts_);
..
In this way the message will be 1, and stop to print
Can sombody give me a hand?
How to get the correct value from .cc file?
Any suggestion will be very thankful!!
In Tcl, every script evaluation produces a result value (which could be the empty value, or could be something else, depending on what happened). That result value may be retrieved from the interpreter context with Tcl_GetStringResult or Tcl_GetObjResult; the former produces (effectively) a const char * and the latter a Tcl_Obj * (i.e., a Tcl value reference; the name Tcl_Obj is for historical reasons). That value is there until the next piece of script is evaluated. Do not modify the Tcl_Obj* returned (except via Tcl's API) unless you know exactly what you're doing (i.e., are writing code to extend Tcl itself). Integers may be efficiently retrieved from a Tcl_Obj* using Tcl_GetIntFromObj (assuming that the value is an integer), floating point numbers with Tcl_GetDoubleFromObj, and a const char * can be retrieved from a Tcl_Obj* using Tcl_GetString or Tcl_GetStringFromObj (slightly different API).
There are also variables in Tcl. You can read those directly with Tcl_GetVar — which returns another const char * — or Tcl_GetVar2Ex (with an ugly name but convenient API) that returns another Tcl_Obj *.
These are all C API functions and all take a Tcl_Interp * (which is a reference to a Tcl evaluation context, which is always bound to a single thread). How they have been wrapped in the C++ API… I can't tell you that.
You are using some weird C++ library you are not giving any information about. However, when using Tcl from a C/C++ program when you evaluate some Tcl code the result is available attached to the interpreter object. You access this using Tcl_GetObjResult and then use an appropriate Tcl_GetIntFromObj or Tcl_GetDoubleFromObj or whatever to read the Tcl_Obj value into some C++ form.
Similar question to Linux equivalent of GetCommandLine and CommandLineToArgv
Is it possible to get the raw command line in linux? The file /proc/self/cmdline is destroyd.
./a.out files="file 1","file 2" param="2"
prints
./a.outfiles=file 1,file 2param=2
which is junk
Escaping command line does work for all arguments but the first.
./a.out files=\"fil 1\",\"fil 2\"\ param=\"2\"
prints
./a.outfiles="fil1","fil2" param="2"
You can't do that. The command line arguments are actually passed to the new process as individual strings. See the linux kernel source:
kernel_execve
Note that kernel_execve(...) takes a const char *argv[] - so there is no such thing as a long string commandline in Linux - it's the layer above that needs to split the arguments into separate components.
Edit: actually, the system call is here:
excve system call
But the statement above still applies. The parameter for argv is already split by the time the kernel gets it from the C-library call to exec.
It is the responsibility of the "starter of the program" (typically a shell, but doesn't have to be) to produce the argv[] array. It will do the "globbing" (expansion of wildcard filenames to the actual files that it matches) and stripping of quotations, variable replacement and so on.
I would also point out that although there are several variants of "exec" in the C library, there is only one way into the kernel. All variants end up in the execve system call that I linked to above. The other variants are simply because the caller may not fancy splitting arguments into invdividual elements, so the C library "helps out" by doing that for the programmer. Similarly for passing an environment array to the new program - if the programmer don't need specific environment, he/she can just call the variant that automatically take the parent process env.
I am trying to convert a std::string Buffer - containing data from a bitmap file - to std::wstring.
I am using MultiByteToWideChar, but that does not work, because the function stops after it encounters the first '\0'-character. Seems like it interprets it as the end of the string.
When i dont pass -1 as the length-parameter, but the real length of the data in the std::string-Buffer, it messes the Unicode-String up with characters that definetly not appeared at that position in the original string...
Do I have to write my own conversion function?
Or maybe shall i keep the data as a casual char-array, because the special-symbols will be converted incorrectly?
With regards
There are many, many things that will fail with this approach. Among other things, extra bytes may be added to your data without your realizing it.
It's odd that your only option takes a std::wstring(). If this is a home-grown library, you should take the trouble to write a new function. If it's not, make sure there's nothing more suitable before writing your own.