I need to write QuizApp using Dart Command-Line Apps. There is a some example from Dartlang.org but all of the related 2013 which is very old. I am using Dart 2 and I need to write a quiz app.
I need to know how to use stdin, stdout, listen and subscription so the app doesn't quit after 1 enter.
How to write QuizApp using Dart Command-Line Apps
You don't really need any use of subscription for a simple quiz command-line application. You only need to use stdin and stdout from the dart:io package since the application does wait for input when you are using 'stdin.readLineSync()'.
I don't want to make you homework so I am only going to give a small example:
import 'dart:io';
main() {
while(true) {
stdout.writeln('Are you OK? (Yes/No)');
if (stdin.readLineSync().toLowerCase() == 'yes') {
stdout.writeln('Then stop trouble me!');
return;
} else {
stdout.write('Let me ask again! ');
}
}
}
Related
I am building a dart program to get the user input. when running the program using the run button I get the output in the output window but I cannot type in it. tried using the terminal to run the program and it worked well. so I want to know what is the right way to take the input from the user?
import 'dart:io';
void main() {
print("enter your name: ");
var name = stdin.readLineSync();
print("hello mrs ${name}");
}
From file -> Preferences -> Settings
search for "Dart: Cli Console"
Dart: Cli Console
Then change the drop-down menu into -terminal-
now you can run again your code and check
you can type in the terminal and your code will work.
In fact the right way using terminal as you did. It's not a fail. It's just how it works.
The answer regarding using the correct vs-code console is correct but you should also check out the dcli package and it's ask function.
Disclaimer: I'm the author of dcli.
I'm new to the Dart functions framework. My goal is to use this package to create several functions and deploy them to Cloud Run (in combination with Firebase, but I guess that's irrelevant to this question).
I've run the quick starts and I've read all of the contents in the docs.
The quick start mentions just one function at a time (e.g. Hello World, Cloud Events, etc..), like this:
import 'package:functions_framework/functions_framework.dart';
import 'package:shelf/shelf.dart';
#CloudFunction()
Response function(Request request) {
return Response.ok('Hello, World!');
}
But as you can see in the quickstarts only one function is handled in a project at a time. How about me wanting to deploy several functions? Should I:
Write several functions in the same project / file, so that the function framework compiles the 'server.dart` by itself
OR
Create a different functions_framework for each function?
Let me be more specific. Should I do the following (option 1 - which makes more sense to me):
import 'dart:math';
import 'package:functions_framework/functions_framework.dart';
import 'package:shelf/shelf.dart';
#CloudFunction()
Response function(Request request) {
return Response.ok('Hello, World!');
}
#CloudFunction()
Response function2(Request request) {
if (Random().nextBool()) {
return Response.ok('Hello, World!');
} else {
return Response.internalServerError();
}
}
Or should I build a different folder by running a build_runner for each function I need in my project?
Is there a difference and/or a best practice?
Thanks in advance.
EDIT. This question is related to the deployment on Cloud Run itself, and not just testing on my own PC. To test my own functions I did the following:
Run dart run build_runner build, so that it updates the server.dart file correctly (I can see that the framework does a lot behind the scenes and that the _nameToFunctionTarget is basically a router);
Run the server in two different terminals, like this: dart run bin/server.dart --port MYPORT --target MYFUNCTION (where MYPORT and MYFUNCTION are either 8080/8081 or function/function2 respectively).
I guess I'm just confused on how to correctly manage this framework once deployed on Cloud Run.
EDIT 2. I just gave up using Dart as a Serverless language or even a Backend language. There's just too much jargon even for the basic things. Any backend framework is either dead, or maintained by one single enthusiast guy (props to him!). This language has not yet received enough love from the Google Team / the community and at this moment in time is basically not possible to go fullstack on just Dart. It's a dream, but it can't be realized now. Furthermore, Dart hardly lacks a proper SDKs to use Firestore, etc., so Firebase isn't an option. I find it easier to just learn NodeJS and exploit the Firebase support for Firebase Functions written in NodeJS, and I'll wait for more support in there in the future, if there ever will be.
The documentation is a bit sparse right now (and I'm new to it also! I couldn't find any good examples, so here goes...)
You can only have a single function that is served. It should be
named 'function' (the type and name can be overriden, see the
cloudevent example dartfn generate cloudevent)
You 'could' have many of these deployed so that each does a specific thing, such as processing cloudevents above, but most people
want something more REST-like (see next)
You need to attach a Router() so that you can have the single entry point (function) handled by specific logic in your code.
Example for Rest
add to pubspec.yaml (in dependencies:) shelf_router: ^1.1.2
delegate the #CloudFunction to use the Router()
functions.dart
import 'package:functions_framework/functions_framework.dart';
import 'package:shelf/shelf.dart';
import 'package:shelf_router/shelf_router.dart';
Router app = Router()
..get('/health', (Request request) {
return Response.ok('healthy');
})
..get('/user/<user>', (Request request, String user) {
// fetch the user... (probably return as json)
return Response.ok('hello $user');
})
..post('/user', (Request request) {
// convert request body to json and persist... (probably return as json)
return Response.ok('saved the user');
});
#CloudFunction()
Future<Response> function(Request request) => app.call(request);
What's the best way to open a file in the default application from Vala?
A bit like how xdg-open works.
I found some existing code in another application, but later on I also found this
GLib.AppInfo.launch_default_for_uri method.
A simple example:
var file = File.new_for_path (file_path);
if (file.query_exists ()) {
try {
AppInfo.launch_default_for_uri (file.get_uri (), null);
} catch (Error e) {
warning ("Unable to launch %s", file_path);
}
}
If you're using GTK, then you've also got Gtk.gtk_show_uri_on_window(), which uses the GLib stuff under the hood.
As far as I know there is only one implementation of the relevant freedesktop.org standards.
That is the reference implementation in xdg-utils:
https://www.freedesktop.org/wiki/Software/xdg-utils/
The tools are written in shell script, for example here is the source code for xdg-open:
https://cgit.freedesktop.org/xdg/xdg-utils/tree/scripts/xdg-open.in
So by far the easiest way is to just call the xdg-open script via Process.spawn_async and friends.
If you insist on using a library function you would have to implement a standard conforming library yourself.
Update:
There are quite a few libraries in various languages that implement some of the freedesktop.org standards, for example here is a list on GitHub:
https://github.com/topics/xdg
For example here is a similar tool to xdg-open written in D:
https://github.com/FreeSlave/mimeapps/blob/master/source/mimeapps.d
What I didn't find so far is a Vala / GLib or plain C library that could easily be used from a Vala application.
Update 2:
Actually it turns out there is something for that purpose in GLib (or more precisely in Gio):
https://valadoc.org/gio-2.0/GLib.AppInfo.launch_default_for_uri_async.html
https://developer.gnome.org/gio/stable/GAppInfo.html
So you should be able to use the GLib.AppInfo.launch_default_for_uri_async method.
I have a fairly lengthy command-line program that requires user input of parameters and then processes using those parameters. What I would like to do is split the program into interactive and non-interactive. I attempted to do that, and intended to have the non-interactive program "call" the interactive program and using the results (parameters), process based on those parameters. The non-interactive part of the program displays results on the console as it processes. I have looked at Process.run and Process.start, but apparently they don't function that way. There is another similar question to this that is about 12-months old, so I thought it worthwhile asking again.
I have looked at Process.run and Process.start, but apparently they don't function that way.
Process.start is what you want here. It can do what you want, but you'll have to become a bit more comfortable with async programming if you aren't already. You'll spawn the process and then asynchronously read and write to the spawned processes stdout and stdin streams.
Your interactive program can do something like this:
// interactive.dart
import 'dart:io';
main() {
var input = stdin.readLineSync();
print(input.toUpperCase());
}
It's using stdin to read input from the command line. Then it outputs the processed result using regular print().
The non-interactive script can spawn and drive that using something like:
import 'dart:convert';
import 'dart:io';
main() {
Process.start("dart", ["interactive.dart"]).then((process) {
process.stdin.writeln("this is the input");
UTF8.decoder.fuse(new LineSplitter()).bind(process.stdout).listen((line) {
print(line);
});
});
}
It uses Process.start to spawn the interactive script. It writes to it using process.stdin. To read the resulting output it has to jump through some hoops to convert the raw byte output to strings for each line, but this is the basic idea.
I want to execute a python or a java class from inside dart.
The following is a snippet which I have used from a stackoverflow question Java
Runtime currentRuntime = Runtime.getRuntime();
Process executeProcess = currentRuntime.exec("cmd /c c:\\somepath\\pythonprogram.py");
I would like to know how to make such calls in dart.
Basically I have a UI where in the user uploads code in java and python.I want to execute the uploaded code from the dart environment instead of creating a routine in java or python on the folder where the code is uploaded.
I personally dont know if this is possible, since dart is purely in a VM.
I want to execute the following command
java abc
from inside dart.
You can simply use Process.run.
import 'dart:io';
main() {
Process.run('java', ['abd']);
}
You can also access to stdout, stderr and exitCode through the resulting ProcessResult :
import 'dart:io';
main() {
Process.run('java', ['abd']).then((ProcessResult pr){
print(pr.exitCode);
print(pr.stdout);
print(pr.stderr);
});
}