Mini shell en python

chedu06 Messages postés 1 Date d'inscription mardi 13 avril 2010 Statut Membre Dernière intervention 13 avril 2010 - 13 avril 2010 à 17:50
didoux95 Messages postés 845 Date d'inscription mardi 25 avril 2006 Statut Membre Dernière intervention 1 août 2017 - 1 mai 2010 à 23:58
Bonsoir,
J'ai un projet qui consiste à réaliser un mini shell en python.Mais pour l'instant je ne sais par où commencer.Explications et pistes (surtout pour le prompt) seront les bienvenues !
Merci par avance !

1 réponse

didoux95 Messages postés 845 Date d'inscription mardi 25 avril 2006 Statut Membre Dernière intervention 1 août 2017 2
1 mai 2010 à 23:58
Bonsoir,

J'ai fais un petit programme en shell il y a quelques temps, dialogue sur le port série (en Java ..).
Je ne sais pas si j'ai eu la meilleurs méthode de développement mais en tout cas ca à aboutit a quelque chose de fonctionnel.
J'ai commencer par faire un module "Parser" qui "parse" les lignes écrite dans la console. Et ensuite j'ai fais un module "Doer" qui exécuter les différentes actions.
Dans l'ensemble, le module "Parser" lit une ligne, l'interprète, et appel la fonction correspondant dans le module Doer.

Voici une infime partie du code du "Parser" :
/**
 *Parses the current command line.
 *
 **/
public void parse () {

//Save the current command line
doer.addCmdLine(cmdLine);

//Get the first char of the command line
String type = "" + this.cmdLine.charAt(0);	

//What kind of command is it ?
if (type.equals("C")) {
configure();
}else if (type.equals("S")) {
send();
}else if (type.equals("R")) {
recieve();
}else if (type.equals("O")) {
other();
}else{
doer.unknownCommand();
}
}


Et un morceau du "Doer" :
/**
 *To display all available commands and the way to use them.
 *
 **/
public void unknownCommand () {

badUsage('C');
badUsage('R');
badUsage('S');
badUsage('O');

}

/**
 *Informs the user about the commands.
 *
 *@param char c The char that corresponds to the command.
 *
 **/
public void badUsage (char c) {

        //En fait on va chercher les informations relatives aux commandes dans des fichiers de description prédéfinit ..
try {

String path = "ressource" + File.separator + "cmds" + File.separator;

Scanner sc = null;
if (c == 'C')
sc = new Scanner(new File(path + "Configure.txt"));
else if (c == 'R')
sc = new Scanner(new File(path + "Recieve.txt"));
else if (c == 'S')
sc = new Scanner(new File(path + "Send.txt"));
else if (c == 'O')
sc = new Scanner(new File(path + "Other.txt"));

commandArea.setText(commandArea.getText() + " \n");

while (sc.hasNextLine())
commandArea.setText(commandArea.getText() + "" + sc.nextLine() + "\n");

}catch (FileNotFoundException fnfe) {}
}


J'espère que ca va t'aider dans ton développement ;)


PS : Le code que je t'ai mis c'est du Java, mais c'est pas important, l'essentiel c'est la structure ..
0
Rejoignez-nous