How to define a new command line program? - node.js

I'm trying to define a command line program which will automatically call a specified Nodejs application.
I want to define it like that:
mycommand newFile
And that will call this application:
node E:/nodejsApp/mycommand.js newFile
mycommand.js is ready now. I want to call it with directly mycommand command on cmd in Windows 10.

For that, create a batch file in your path (ex: C:\WINDOWS\System32) with that code:
node E:\nodejsApp\mycommand.js %1
Save it as the command you want to create + the batch extension ([command].bat)
Another option will be, as aschipfl mentioned, use the doskey command creating an autorun. Note that I recommend creating the batch file, as running a command at starting of CMD will slow down his starting.
For the AutoRun, create a registry key at HKCU\Software\Microsoft\Command Processor called AutoRun, type REG_MULTI_SZ and write the value of doskey mycommand=node E:\nodejsApp\mycommand.js %1

Related

Use python, to open cmd window with the directory supplied as an argument as the current directory

I want to use Python to open a directory in a Win10 cmd window, and keep the window open.
I made a batch file named: open_dir_in_cmd_window.CMD:
CD /D %1
I tested that batch file successfully, by creating another batch file named, Test.cmd:
Rem "open_dir_in_cmd_window.CMD" "f:\backup"
"open_dir_in_cmd_window.CMD" "f:\backup"
A very helpful webpage provides the following example, which I seem unable to follow correctly:
Spaces in Program Path + parameters with spaces:
CMD /k ""c:\batch files\demo.cmd" "Parameter 1 with space" "Parameter2 with space""
I made a python script, which contains the following lines, which alas, triggers an error message:
import subprocess
subprocess.run(cmd /k "E:\open_dir_in_cmd_window.CMD f:\backup")
When I open a Command Prompt window and run:
"C:\ProgramData\Anaconda3\python.exe" E:\open_dir.py
I get an error message, SyntaxError: invalid syntax, with this:
subprocess.run(cmd /k "E:\open_dir_in_cmd_window.CMD f:\backup")
^
I've tried many different permutations of double quoting and can't figure out the right way to do it.
I have spent many hours hunting on the web and trying to figure this out and I do not know what to do.
Any suggestions would be appreciated.

How do you run a standalone ijs j file on Linux?

I have been using j for a few weeks now and loving it. However, all of my work has been in the ijconsole. Does j provide a way to run .ijs files without using load? Similar to how you can simply run $python my_file.py?
I know for windows there exists jconsole.exe, but for Linux and OSx there doesn't seem to be the same option?
You should be able to run bin/jconsole with the .ijs file as the first command line argument.
Here's an example session, copied out of my terminal:
~/j64-807$ cat ex.ijs
d =: 1+1
~/j64-807$ ./bin/jconsole ex.ijs
d
2
I figured out how to get my desired behavior from
https://www.jsoftware.com/help/user/hashbang.htm
The basic idea is to create a "unix script" that points to jconsole by using
#!/home/fred/j64-807/bin/jconsole
at the top of the .ijs file.
Then, you can echo any output you wish or use ARGV to read input.
Finally, call chmod +x your_script.ijs and run using ./your_script.ijs
You can copy the j interpreter files to new projects on servers etc and call them using bash.
So, a final example would be
#!/home/fred/j64-807/bin/jconsole
echo +/*:0".>,.2}.ARGV
exit''
Which computes the sum of squares of digits from command line arguments

can we pass options to tcl source command in tcl 8.5

I am using this command to source get.tcl file and giving options 'verbose' and 'instant':
source -verbose -instant get.tcl
the above command worked for me in tcl 8.4 but showing this error in tcl 8.5
source (script wrong # args: should be "source_orig ?-encoding name?
fileName"
if I write only
source get.tcl
It get passed in tcl 8.5
Is there any change related to this in tcl 8.5?
The source command only accepts one option (since 8.5), -encoding, which is used to specify what encoding the file being read is in (instead of the default guess of encoding as returned by encoding system). All it does is read the file into memory and (internally-equivalent-to-) eval the contents.
You can write to any variable you want prior to doing the source, including global variables like argv. With that (plus appropriate use of uplevel and catch, as required, and maybe also interp create) you can simulate running the script as a subprocess. But it's probably easier to not have the file expect to be handling arguments like that, and instead for it to define a command that you call immediately after the sourcing.
You can pass arguments to your sourced file by doing the following:
set ::argv [list -verbose -instant]
source get.tcl
I recommend using:
set ::argv [list -- -verbose -instant]
The -- will stop tclsh from processing any arguments after the --.
Sometimes tclsh will recognize an argument that is meant for your
program and process it. Your programs will need to know about
the -- and handle it appropriately.

Cmake add command line argument to binary

I create a binary myBinary via cmake/CMakeLists.txt.
I would like to "include" default options on my binary.
In other words, I want my binary to be called with myBinary --option myopt even when I just run ./myBinary
How can I do that?
CMake does not have built-in support for you you want to do.
One solution is to do as #Youka said - change the source code of your program.
Another solution that I have used sometimes is to autogenerate a script that executes an executable:
# Create startup script
MACRO(GEN_START_SCRIPT binName)
# Generate content
SET(fileContent
"#!/bin/bash\n"
"\n"
"# This startup script is auto generated - do not modify!\n"
"\n"
"${binName} -a 23 -b 34 -c 976\n"
"\n"
)
# Write to file
SET(fileName ${CMAKE_CURRENT_BINARY_DIR}/${binName}.sh)
FILE(WRITE ${fileName} ${fileContent})
ENDMACRO()
Then call the macro after defining your executable:
ADD_EXECUTABLE(myBinary file1.c file.2)
GEN_START_SCRIPT(myBinary)
You can of course add other stuff to the script, like environment variables etc.
If you're in control of the sources and you want different default behavior... change the sources!
This is in no way a build system issue (CMake or otherwise).

How do I set up a preload file for node?

Is there a way to preload some file before each time I run node (interactively), just like .vimrc, .bash_profile, etc.?
I use node mainly interactively, and I use the module CSV a lot, is there a way to avoid typing require('./csv') every time I start node?
Create an initialization file (for example ~/.noderc):
var csv = require('csv');
// put a blank line at the end of the file
Now add this line to your shell config (.bashrc / .zshrc / whatever shell you use):
alias nodei="cat ~/.noderc - | node -i"
VoilĂ !
#Ilan Frumer provided one way to do it. I think I'll give another choice here: build a REPL of your own.
From their documentation. You can find a way to write a repl of your own. You can add whatever scripts before and after the interations of it, and even use some advance API's.
For example, I created a file called .noderc.js under ~ as follows
repl = require('repl');
myFunc = function(){
console.log("Hello, there!");
};
repl.start("> ");
And you can go ahead and alias nodei="node ~/.noderc.js",
$ nodei
> myFunc()
Hello, there!
undefined

Resources