2 Replies - 294 Views - Last Post: 19 February 2018 - 09:17 AM

#1 bb8   User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 335
  • Joined: 31-January 16

oop: parsing command for calling a command with arguments

Posted 19 February 2018 - 07:23 AM

i'm writing an application using PyQt5. the application controller class has a main window (as a member), etc. the main window has a member - a dock-able window which has a text edit as its widget. the user will be able to input text there (i call those commands).

i have a commnd engine class which has the following functionalities:
  • save command name to command object mapping
  • remove entry from the map
  • call the command


the command has the following methods:
  • validate command parameters
  • execute the command
  • above functions are called from another member function - call() which should be called by command engine

this is a basic command pattern implementation, where invoker and receiver are the same - the command engine.

now the problem: user inputted a text. the command window will notify the application controller (via Qt's signal) that a text was inputted. what should happen next? i have ONE string which contains the command and its arguments. i need to pass the command name (argument 1) and command parameters (argument 2) to the command engine so it will find the appropriate command from map via the passed name, and call the command's call(), passing the arguments provided.

but the problem is that when i have only one string, not knowing where command name ends and where arguments begin.

my question is: who should "prepare" the actual command name and arguments for the command engine? the command itself, the command engine or the application controller?

p.s. sorry for not providing any code, i'm on my phone now, i hope i explained clearly. will provide code if needed.

Is This A Good Question/Topic? 0
  • +

Replies To: oop: parsing command for calling a command with arguments

#2 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6216
  • View blog
  • Posts: 21,452
  • Joined: 05-May 12

Re: oop: parsing command for calling a command with arguments

Posted 19 February 2018 - 07:49 AM

There are many ways to attack this. This is just my opinion, but, I would let each command decide if it knows how to handle the entire string or not. The command engine takes care of breaking out the first word of the string to determine which command(s) should be queried. The controller merely asks the command engine to determine which command needs to be invoked, but the controller actually performs the invocation of the command (on the assumption that the command may need access to the model and the view that controller knows about).
Was This Post Helpful? 1
  • +
  • -

#3 bb8   User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 335
  • Joined: 31-January 16

Re: oop: parsing command for calling a command with arguments

Posted 19 February 2018 - 09:17 AM

the command may consist of 3 words or 2. besides that, when the controller gets notified about that there is a command available, there's only the string, no command object is known to the controller. so i can't really tell any command to prepare itself. the command engine is the one that should get the command from its internal map storage and execute it.

EDIT: should i have a command manager/controller that knows about all commands and splits the string appropriately? but then, what if the input wasn't a valid command name? this class would notify the app controller about it, but that should be the work of the command engine to do that...

This post has been edited by bb8: 19 February 2018 - 09:27 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1