Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to pass command-line arguments to pyret program #5

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
*.sw*
node_modules
pyret-lang
package-lock.json
.pyret
*.jarr
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ deploy:
provider: npm
email: [email protected]
api_key:
secure: o8BnblLlSjc0QT89NxDbbaBn/5q1LOFi0JRhiFCqLWw6lwJlEWgg4t6Stnv+/i5sCnnTEnCwdJoB/8OK7nNeQOlfYfwyu/mHGybOOrmTi7+qxL8OlXXDku72BdxTPYmUyB7f8jVDbpt8k3TL76YWpi8s+h9O0sUELM8QVaT2CI2nLNFdmzwErUSOJF5+SP+Eeo2XclQt1BvoM6V/GKb6XBDx4zV+gXinjyCggb6bNPdbvbQ0zXbVrSR0Dxrzuj9E3o16DhPkP8wvNH4ujAfZhDKvZ05MgxMecjUXuBJgTS84L5DS10HapQfB9VgRWvgoa/oeP60xBLDGjc6I66eP3UyxQkjC4AHyuXLkZreL88kjMgD1yGTkIlY7uvEgr/uzj1WqTuc9Ngwiaf/y5sxHmTA1rly68sYalNLAtmA+eGjfWg6qRd6ZuECdrBb+Yurn/VKidOdm1HPuS4OiVN+mKG0KrBDmwcIVt9Vw3RjqmnUDuGB1cGWVUvXwcPou66i8m2a7OMPlpPFbTSbWy3CuoUIasfDx1toGO0W3Pvwrqd1kZ1gMHZKQJKrYxvZixUXxFkT51GlnLdXyrky8So9e697vZx5exWqvVem8t9wHOEAvRxEDi0XIFGh/4jectyDoZGdDa4RjA/au2+B9jVi6kDa0zlmRImg4v7BoZM2hEVY=
secure: "L9yBu/F7D9cRF3vskTB62XEjSuDUDzzKBoAlZdKTCyzwE2gOXarYNFeT+/JKw5LXLlQRWIzj+GAN3/qIogKGN08YdaGpOqcIq4R6UYcGnwXJqr+SWifZhNbAgF9e5sVi4p793X2/XLVe5UGh1bhBGVw8l7Xx9LOWHMbK1REYJhVIIXNHrFY1HmPJKOZW3kTiuccwYDRDkO3WwB3TCHm9ne4FCaWi0juWV9BQjFQMWa9eqSyO4PIBdRWymqYYQyOtZqiWeOMdpwAsipAsJtZ7Y6FhM7PoLiKq+8CLqcb9+LHFp0LyLf4erPO9F224bMJkLgRQyO/gi1KZ1BDEdsKeRh+dE/nWFdu5Cz0+GdbsC/UDLW8xljNULTGVset1rWBtgmiPzsDlcA1VdLxeUfQAVQqC+WJH8ZelcWJ/K9unTHSXKyXAYYDFt2vNEl5G1SSfgArgmi/0VF6dAcappHij68H+ct7ci1lgjnfO/NSmTeebzmOdv0kRPo2O8UQEMHvtcI/ClFbyGJOv23FjGlq5e5MyLsX5WCEyBmgMzqzSWeq11/HAbYd706HsRbkhJLyoCl2o1NUmkkDN607lj/mnJEQUmaVr7jlIR/my6vZKmj97xsp9fkjkwPniPcdIfBmACbOpj3T7KmuXVevRhW/lqsJQEjODFIhpefg1M5O+PI4="
7 changes: 7 additions & 0 deletions ahoy-world-args.arr
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import cmdline-lib as CL
args = CL.command-line-arguments()
if args.length() >= 2:
print("Ahoy " + args.get(1) + "!\n")
else:
print("Ahoy world!\n")
end
13 changes: 8 additions & 5 deletions client-lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@ function start(options) {
//process.exit(0);
}

function runProgram(path) {
log("Executing program: ", path);
const proc = childProcess.spawn("node", [path], {stdio: 'inherit'});
function runProgram(path, argv) {
log("Executing program: ", path, " with ", argv);
const proc = childProcess.spawn("node", [path].concat(argv), {stdio: 'inherit'});
proc.on('close', function(code) {
process.exit(code);
});
Expand Down Expand Up @@ -176,12 +176,15 @@ function start(options) {
else if(parsed.type === "compile-success") {
log("Successful compile response");
if(!options.meta.norun) {
process.nextTick(() => runProgram(options["pyret-options"]["outfile"]));
var argv = options["_unknown"] || [];
if (argv.length > 0 && argv[0] == "--") {
argv = argv.slice(1);
}
process.nextTick(() => runProgram(options["pyret-options"]["outfile"], argv));
}
}
});


var forMessage = { command: "compile", compileOptions: JSON.stringify(options['pyret-options']) };
client.send(JSON.stringify(forMessage));
});
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pyret-npm",
"version": "0.0.17",
"version": "0.0.28-alpha",
"description": "The CLI for the Pyret programming language",
"files": [
"pyret.js",
Expand Down
38 changes: 34 additions & 4 deletions pyret.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ const usages = [
'',
'',
' This command compiled and ran {underline ahoy-world.arr}. The first time, this will take a few seconds as a server starts up in the background, in order to make future compiles fast.',

'',
'',
' It\'s worth noting that the file is compiled into a standalone JavaScript file with the {underline .jarr} extension:',
Expand All @@ -42,7 +41,34 @@ const usages = [
'',
'',
' Most uses (e.g. for homework) only need to use the {bold pyret} command directly on {underline .arr} files, but there are several other options that can be provided.',

'',
'',
' You can pass arguments to your pyret program by adding {bold --} followed by the command-line arguments:',
]
},
{
content: {
options: {
noTrim: true
},
data: [
{col: '$ cat ahoy-world-args.arr'},
{col: 'import cmdline-lib as CL'},
{col: 'args = CL.command-line-arguments()'},
{col: 'if args.length() >= 2:'},
{col: ' print("Ahoy " + args.get(1) + "!\\\\n")'},
{col: 'else:'},
{col: ' print("Ahoy world!\\\\n")'},
{col: 'end'},
{col: '$ pyret -qk ahoy-world-args.arr {bold -- Captain}'},
{col: 'Ahoy Captain!'},
]
}
},
{
content: [
'',
' This command suppresses the progress indication, disables checks, and passes one command-line argument to {underline ahoy-world-args.arr}: {bold "Captain"} (index 1). The {bold "--"} argument is only needed so that {bold pyret} does not interpret it. It should be noted that the index 0 argument contains the path to the compiled {underline .jarr} file.',
]
},
{
Expand Down Expand Up @@ -253,7 +279,7 @@ function printVersion() {
}

try {
options = commandLineArgs(optionDefinitions);
options = commandLineArgs(optionDefinitions, {stopAtFirstUnknown: true});
if(options.meta.help) {
printUsage();
process.exit(0);
Expand All @@ -276,5 +302,9 @@ if(!options["pyret-options"]["outfile"] && options["pyret-options"]["program"])
}
}

pyretClient.start(options);
if (options["_unknown"] && !options["pyret-options"]["program"]) {
printUsage();
process.exit(1);
}

pyretClient.start(options);