From 50b649ad3f4e0f16e9440a6ed6ed866c73208854 Mon Sep 17 00:00:00 2001
From: Florian Pose <fp@igh-essen.com>
Date: Thu, 11 Mar 2010 15:03:52 +0100
Subject: [PATCH] Open master device as late as possible.

---
 tool/Command.cpp         | 41 +++++++++++++++++++++++++++++++++--
 tool/Command.h           | 14 ++++--------
 tool/CommandAlias.cpp    |  6 ++++--
 tool/CommandCStruct.cpp  |  6 ++++--
 tool/CommandConfig.cpp   | 10 +++++----
 tool/CommandData.cpp     |  6 ++++--
 tool/CommandDebug.cpp    |  6 ++++--
 tool/CommandDomains.cpp  | 10 +++++----
 tool/CommandDownload.cpp |  6 ++++--
 tool/CommandEoe.cpp      |  8 ++++---
 tool/CommandFoeRead.cpp  |  6 ++++--
 tool/CommandFoeWrite.cpp |  6 ++++--
 tool/CommandGraph.cpp    |  6 ++++--
 tool/CommandMaster.cpp   |  6 ++++--
 tool/CommandPdos.cpp     |  8 ++++---
 tool/CommandRegRead.cpp  |  6 ++++--
 tool/CommandRegWrite.cpp |  6 ++++--
 tool/CommandSdos.cpp     |  8 ++++---
 tool/CommandSiiRead.cpp  |  6 ++++--
 tool/CommandSiiWrite.cpp |  6 ++++--
 tool/CommandSlaves.cpp   |  8 ++++---
 tool/CommandSoeRead.cpp  |  6 ++++--
 tool/CommandSoeWrite.cpp |  6 ++++--
 tool/CommandStates.cpp   |  6 ++++--
 tool/CommandUpload.cpp   |  6 ++++--
 tool/CommandXml.cpp      |  6 ++++--
 tool/main.cpp            | 46 ++++++----------------------------------
 27 files changed, 154 insertions(+), 107 deletions(-)

diff --git a/tool/Command.cpp b/tool/Command.cpp
index 82590d13..8b9da9b7 100644
--- a/tool/Command.cpp
+++ b/tool/Command.cpp
@@ -29,6 +29,21 @@
 
 #include "Command.h"
 #include "MasterDevice.h"
+#include "NumberListParser.h"
+
+/*****************************************************************************/
+
+class MasterIndexParser:
+    public NumberListParser
+{
+    unsigned int getMax()
+    {
+        MasterDevice dev;
+        dev.setIndex(0U);
+        dev.open(MasterDevice::Read);
+        return dev.getMasterCount() - 1;
+    };
+};
 
 /*****************************************************************************/
 
@@ -47,9 +62,9 @@ Command::~Command()
 
 /*****************************************************************************/
 
-void Command::setMasterIndices(const MasterIndexList &indices)
+void Command::setMasters(const string &m)
 {
-    masterIndices = indices;
+    masters = m;
 };
 
 /*****************************************************************************/
@@ -172,6 +187,28 @@ void Command::throwSingleSlaveRequired(unsigned int size) const
 
 /*****************************************************************************/
 
+Command::MasterIndexList Command::getMasterIndices() const
+{
+	MasterIndexList indices;
+
+    try {
+        MasterIndexParser p;
+        indices = p.parse(masters.c_str());
+    } catch (MasterDeviceException &e) {
+		stringstream err;
+		err << "Failed to obtain number of masters: " << e.what();
+		throwCommandException(err);
+    } catch (runtime_error &e) {
+		stringstream err;
+        err << "Invalid master argument '" << masters << "': " << e.what();
+		throwInvalidUsageException(err);
+    }
+
+	return indices;
+}
+
+/*****************************************************************************/
+
 Command::SlaveList Command::selectedSlaves(MasterDevice &m)
 {
     ec_ioctl_master_t master;
diff --git a/tool/Command.h b/tool/Command.h
index a51aa171..b98001f8 100644
--- a/tool/Command.h
+++ b/tool/Command.h
@@ -85,8 +85,9 @@ class Command
         const string &getBriefDescription() const;
 
         typedef list<unsigned int> MasterIndexList;
-        void setMasterIndices(const MasterIndexList &);
-        const MasterIndexList &getMasterIndices() const;
+        void setMasters(const string &);
+        MasterIndexList getMasterIndices() const;
+
         enum Verbosity {
             Quiet,
             Normal,
@@ -137,7 +138,7 @@ class Command
     private:
         string name;
         string briefDesc;
-        MasterIndexList masterIndices;
+        string masters;
         Verbosity verbosity;
         int alias;
         int position;
@@ -165,13 +166,6 @@ inline const string &Command::getBriefDescription() const
 
 /****************************************************************************/
 
-inline const Command::MasterIndexList &Command::getMasterIndices() const
-{
-    return masterIndices;
-}
-
-/****************************************************************************/
-
 inline Command::Verbosity Command::getVerbosity() const
 {
     return verbosity;
diff --git a/tool/CommandAlias.cpp b/tool/CommandAlias.cpp
index cd7fd8bd..c6d588f3 100644
--- a/tool/CommandAlias.cpp
+++ b/tool/CommandAlias.cpp
@@ -81,6 +81,7 @@ void CommandAlias::execute(const StringVector &args)
     uint16_t alias;
     stringstream err, strAlias;
     int number;
+	MasterIndexList masterIndices;
     SlaveList slaves;
     SlaveList::const_iterator si;
 
@@ -99,11 +100,12 @@ void CommandAlias::execute(const StringVector &args)
     }
     alias = number;
 
-    if (getMasterIndices().size() != 1) {
+	masterIndices = getMasterIndices();
+    if (masterIndices.size() != 1) {
         err << getName() << " requires to select a single master!";
         throwInvalidUsageException(err);
     }
-    MasterDevice m(getMasterIndices().front());
+    MasterDevice m(masterIndices.front());
     m.open(MasterDevice::ReadWrite);
     slaves = selectedSlaves(m);
     
diff --git a/tool/CommandCStruct.cpp b/tool/CommandCStruct.cpp
index b49b72e0..a223df20 100644
--- a/tool/CommandCStruct.cpp
+++ b/tool/CommandCStruct.cpp
@@ -70,6 +70,7 @@ string CommandCStruct::helpString() const
 
 void CommandCStruct::execute(const StringVector &args)
 {
+	MasterIndexList masterIndices;
     SlaveList slaves;
     SlaveList::const_iterator si;
 
@@ -79,9 +80,10 @@ void CommandCStruct::execute(const StringVector &args)
         throwInvalidUsageException(err);
     }
 
+	masterIndices = getMasterIndices();
     MasterIndexList::const_iterator mi;
-    for (mi = getMasterIndices().begin();
-            mi != getMasterIndices().end(); mi++) {
+    for (mi = masterIndices.begin();
+            mi != masterIndices.end(); mi++) {
         MasterDevice m(*mi);
         m.open(MasterDevice::Read);
         slaves = selectedSlaves(m);
diff --git a/tool/CommandConfig.cpp b/tool/CommandConfig.cpp
index 6ee8dec4..4cf901b2 100644
--- a/tool/CommandConfig.cpp
+++ b/tool/CommandConfig.cpp
@@ -104,8 +104,9 @@ string CommandConfig::helpString() const
  */
 void CommandConfig::execute(const StringVector &args)
 {
-    ConfigList configs;
+	MasterIndexList masterIndices;
     bool doIndent;
+    ConfigList configs;
 
     if (args.size()) {
         stringstream err;
@@ -113,10 +114,11 @@ void CommandConfig::execute(const StringVector &args)
         throwInvalidUsageException(err);
     }
 
-    doIndent = getMasterIndices().size() > 1;
+	masterIndices = getMasterIndices();
+    doIndent = masterIndices.size() > 1;
     MasterIndexList::const_iterator mi;
-    for (mi = getMasterIndices().begin();
-            mi != getMasterIndices().end(); mi++) {
+    for (mi = masterIndices.begin();
+            mi != masterIndices.end(); mi++) {
         MasterDevice m(*mi);
         m.open(MasterDevice::Read);
         configs = selectedConfigs(m);
diff --git a/tool/CommandData.cpp b/tool/CommandData.cpp
index f7e3952e..c172a976 100644
--- a/tool/CommandData.cpp
+++ b/tool/CommandData.cpp
@@ -66,6 +66,7 @@ string CommandData::helpString() const
 
 void CommandData::execute(const StringVector &args)
 {
+	MasterIndexList masterIndices;
     DomainList domains;
     DomainList::const_iterator di;
 
@@ -75,9 +76,10 @@ void CommandData::execute(const StringVector &args)
         throwInvalidUsageException(err);
     }
 
+	masterIndices = getMasterIndices();
     MasterIndexList::const_iterator mi;
-    for (mi = getMasterIndices().begin();
-            mi != getMasterIndices().end(); mi++) {
+    for (mi = masterIndices.begin();
+            mi != masterIndices.end(); mi++) {
         MasterDevice m(*mi);
         m.open(MasterDevice::Read);
         domains = selectedDomains(m);
diff --git a/tool/CommandDebug.cpp b/tool/CommandDebug.cpp
index ea0582cf..0d31441e 100644
--- a/tool/CommandDebug.cpp
+++ b/tool/CommandDebug.cpp
@@ -68,6 +68,7 @@ string CommandDebug::helpString() const
 
 void CommandDebug::execute(const StringVector &args)
 {
+	MasterIndexList masterIndices;
     stringstream str;
     int debugLevel;
     
@@ -87,9 +88,10 @@ void CommandDebug::execute(const StringVector &args)
         throwInvalidUsageException(err);
     }
 
+	masterIndices = getMasterIndices();
     MasterIndexList::const_iterator mi;
-    for (mi = getMasterIndices().begin();
-            mi != getMasterIndices().end(); mi++) {
+    for (mi = masterIndices.begin();
+            mi != masterIndices.end(); mi++) {
         MasterDevice m(*mi);
         m.open(MasterDevice::ReadWrite);
         m.setDebug(debugLevel);
diff --git a/tool/CommandDomains.cpp b/tool/CommandDomains.cpp
index 09b612be..738868a1 100644
--- a/tool/CommandDomains.cpp
+++ b/tool/CommandDomains.cpp
@@ -92,9 +92,10 @@ string CommandDomains::helpString() const
 
 void CommandDomains::execute(const StringVector &args)
 {
+	MasterIndexList masterIndices;
+    bool doIndent;
     DomainList domains;
     DomainList::const_iterator di;
-    bool doIndent;
 
     if (args.size()) {
         stringstream err;
@@ -102,10 +103,11 @@ void CommandDomains::execute(const StringVector &args)
         throwInvalidUsageException(err);
     }
 
-    doIndent = getMasterIndices().size() > 1;
+	masterIndices = getMasterIndices();
+    doIndent = masterIndices.size() > 1;
     MasterIndexList::const_iterator mi;
-    for (mi = getMasterIndices().begin();
-            mi != getMasterIndices().end(); mi++) {
+    for (mi = masterIndices.begin();
+            mi != masterIndices.end(); mi++) {
         MasterDevice m(*mi);
         m.open(MasterDevice::Read);
         domains = selectedDomains(m);
diff --git a/tool/CommandDownload.cpp b/tool/CommandDownload.cpp
index ee008f13..fbbfcb44 100644
--- a/tool/CommandDownload.cpp
+++ b/tool/CommandDownload.cpp
@@ -85,6 +85,7 @@ string CommandDownload::helpString() const
 void CommandDownload::execute(const StringVector &args)
 {
     stringstream strIndex, strSubIndex, err;
+	MasterIndexList masterIndices;
     ec_ioctl_slave_sdo_download_t data;
     unsigned int number;
     const DataType *dataType = NULL;
@@ -114,11 +115,12 @@ void CommandDownload::execute(const StringVector &args)
     }
     data.sdo_entry_subindex = number;
 
-    if (getMasterIndices().size() != 1) {
+	masterIndices = getMasterIndices();
+    if (masterIndices.size() != 1) {
         err << getName() << " requires to select a single master!";
         throwInvalidUsageException(err);
     }
-    MasterDevice m(getMasterIndices().front());
+    MasterDevice m(masterIndices.front());
     m.open(MasterDevice::ReadWrite);
     slaves = selectedSlaves(m);
     if (slaves.size() != 1) {
diff --git a/tool/CommandEoe.cpp b/tool/CommandEoe.cpp
index d972de39..67ad0346 100644
--- a/tool/CommandEoe.cpp
+++ b/tool/CommandEoe.cpp
@@ -63,6 +63,7 @@ string CommandEoe::helpString() const
 
 void CommandEoe::execute(const StringVector &args)
 {
+	MasterIndexList masterIndices;
     ec_ioctl_master_t master;
     unsigned int i;
     ec_ioctl_eoe_handler_t eoe;
@@ -75,11 +76,12 @@ void CommandEoe::execute(const StringVector &args)
         throwInvalidUsageException(err);
     }
 
-    doIndent = getMasterIndices().size();
+	masterIndices = getMasterIndices();
+    doIndent = masterIndices.size();
     indent = doIndent ? "  " : "";
     MasterIndexList::const_iterator mi;
-    for (mi = getMasterIndices().begin();
-            mi != getMasterIndices().end(); mi++) {
+    for (mi = masterIndices.begin();
+            mi != masterIndices.end(); mi++) {
         MasterDevice m(*mi);
         m.open(MasterDevice::Read);
         m.getMaster(&master);
diff --git a/tool/CommandFoeRead.cpp b/tool/CommandFoeRead.cpp
index ab4b5041..1afc7855 100644
--- a/tool/CommandFoeRead.cpp
+++ b/tool/CommandFoeRead.cpp
@@ -76,6 +76,7 @@ string CommandFoeRead::helpString() const
 
 void CommandFoeRead::execute(const StringVector &args)
 {
+	MasterIndexList masterIndices;
     SlaveList slaves;
     ec_ioctl_slave_t *slave;
     ec_ioctl_slave_foe_t data;
@@ -87,11 +88,12 @@ void CommandFoeRead::execute(const StringVector &args)
         throwInvalidUsageException(err);
     }
 
-    if (getMasterIndices().size() != 1) {
+	masterIndices = getMasterIndices();
+    if (masterIndices.size() != 1) {
         err << getName() << " requires to select a single master!";
         throwInvalidUsageException(err);
     }
-    MasterDevice m(getMasterIndices().front());
+    MasterDevice m(masterIndices.front());
     m.open(MasterDevice::Read);
     slaves = selectedSlaves(m);
 
diff --git a/tool/CommandFoeWrite.cpp b/tool/CommandFoeWrite.cpp
index 11f0fedb..c1c3139b 100644
--- a/tool/CommandFoeWrite.cpp
+++ b/tool/CommandFoeWrite.cpp
@@ -82,6 +82,7 @@ string CommandFoeWrite::helpString() const
 
 void CommandFoeWrite::execute(const StringVector &args)
 {
+	MasterIndexList masterIndices;
     stringstream err;
     ec_ioctl_slave_foe_t data;
     ifstream file;
@@ -93,11 +94,12 @@ void CommandFoeWrite::execute(const StringVector &args)
         throwInvalidUsageException(err);
     }
 
-    if (getMasterIndices().size() != 1) {
+	masterIndices = getMasterIndices();
+    if (masterIndices.size() != 1) {
         err << getName() << " requires to select a single master!";
         throwInvalidUsageException(err);
     }
-    MasterDevice m(getMasterIndices().front());
+    MasterDevice m(masterIndices.front());
 
     if (args[0] == "-") {
         loadFoeData(&data, cin);
diff --git a/tool/CommandGraph.cpp b/tool/CommandGraph.cpp
index 639c4dd6..78cf2b2f 100644
--- a/tool/CommandGraph.cpp
+++ b/tool/CommandGraph.cpp
@@ -67,6 +67,7 @@ string CommandGraph::helpString() const
 
 void CommandGraph::execute(const StringVector &args)
 {
+	MasterIndexList masterIndices;
     ec_ioctl_master_t master;
     unsigned int i;
     typedef vector<ec_ioctl_slave_t> SlaveVector;
@@ -90,12 +91,13 @@ void CommandGraph::execute(const StringVector &args)
         throwInvalidUsageException(err);
     }
 
-    if (getMasterIndices().size() != 1) {
+	masterIndices = getMasterIndices();
+    if (masterIndices.size() != 1) {
         stringstream err;
         err << getName() << " requires to select a single master!";
         throwInvalidUsageException(err);
     }
-    MasterDevice m(getMasterIndices().front());
+    MasterDevice m(masterIndices.front());
     m.open(MasterDevice::Read);
     m.getMaster(&master);
 
diff --git a/tool/CommandMaster.cpp b/tool/CommandMaster.cpp
index 2a406e6b..fb545437 100644
--- a/tool/CommandMaster.cpp
+++ b/tool/CommandMaster.cpp
@@ -67,6 +67,7 @@ string CommandMaster::helpString() const
 
 void CommandMaster::execute(const StringVector &args)
 {
+	MasterIndexList masterIndices;
     ec_ioctl_master_t data;
     stringstream err;
     unsigned int i, j;
@@ -79,9 +80,10 @@ void CommandMaster::execute(const StringVector &args)
         throwInvalidUsageException(err);
     }
 
+	masterIndices = getMasterIndices();
     MasterIndexList::const_iterator mi;
-    for (mi = getMasterIndices().begin();
-            mi != getMasterIndices().end(); mi++) {
+    for (mi = masterIndices.begin();
+            mi != masterIndices.end(); mi++) {
         MasterDevice m(*mi);
         m.open(MasterDevice::Read);
         m.getMaster(&data);
diff --git a/tool/CommandPdos.cpp b/tool/CommandPdos.cpp
index 13098c77..ccf7d97e 100644
--- a/tool/CommandPdos.cpp
+++ b/tool/CommandPdos.cpp
@@ -91,6 +91,7 @@ string CommandPdos::helpString() const
 
 void CommandPdos::execute(const StringVector &args)
 {
+	MasterIndexList masterIndices;
     SlaveList slaves;
     SlaveList::const_iterator si;
     bool showHeader, multiMaster;
@@ -101,10 +102,11 @@ void CommandPdos::execute(const StringVector &args)
         throwInvalidUsageException(err);
     }
 
-    multiMaster = getMasterIndices().size() > 1;
+	masterIndices = getMasterIndices();
+    multiMaster = masterIndices.size() > 1;
     MasterIndexList::const_iterator mi;
-    for (mi = getMasterIndices().begin();
-            mi != getMasterIndices().end(); mi++) {
+    for (mi = masterIndices.begin();
+            mi != masterIndices.end(); mi++) {
         MasterDevice m(*mi);
         m.open(MasterDevice::Read);
         slaves = selectedSlaves(m);
diff --git a/tool/CommandRegRead.cpp b/tool/CommandRegRead.cpp
index 8847ba7b..65851865 100644
--- a/tool/CommandRegRead.cpp
+++ b/tool/CommandRegRead.cpp
@@ -79,6 +79,7 @@ string CommandRegRead::helpString() const
 
 void CommandRegRead::execute(const StringVector &args)
 {
+	MasterIndexList masterIndices;
     SlaveList slaves;
     ec_ioctl_slave_reg_t data;
     stringstream strOffset, err;
@@ -140,11 +141,12 @@ void CommandRegRead::execute(const StringVector &args)
         throwInvalidUsageException(err);
     }
     
-    if (getMasterIndices().size() != 1) {
+	masterIndices = getMasterIndices();
+    if (masterIndices.size() != 1) {
         err << getName() << " requires to select a single master!";
         throwInvalidUsageException(err);
     }
-    MasterDevice m(getMasterIndices().front());
+    MasterDevice m(masterIndices.front());
     m.open(MasterDevice::Read);
     slaves = selectedSlaves(m);
 
diff --git a/tool/CommandRegWrite.cpp b/tool/CommandRegWrite.cpp
index 0c6ed512..6de5034e 100644
--- a/tool/CommandRegWrite.cpp
+++ b/tool/CommandRegWrite.cpp
@@ -81,6 +81,7 @@ string CommandRegWrite::helpString() const
 
 void CommandRegWrite::execute(const StringVector &args)
 {
+	MasterIndexList masterIndices;
     stringstream strOffset, err;
     ec_ioctl_slave_reg_t data;
     ifstream file;
@@ -100,11 +101,12 @@ void CommandRegWrite::execute(const StringVector &args)
         throwInvalidUsageException(err);
     }
   
-    if (getMasterIndices().size() != 1) {
+	masterIndices = getMasterIndices();
+    if (masterIndices.size() != 1) {
         err << getName() << " requires to select a single master!";
         throwInvalidUsageException(err);
     }
-    MasterDevice m(getMasterIndices().front());
+    MasterDevice m(masterIndices.front());
 
     if (getDataType().empty()) {
         if (args[1] == "-") {
diff --git a/tool/CommandSdos.cpp b/tool/CommandSdos.cpp
index 772c306e..d194e218 100644
--- a/tool/CommandSdos.cpp
+++ b/tool/CommandSdos.cpp
@@ -89,6 +89,7 @@ string CommandSdos::helpString() const
 
 void CommandSdos::execute(const StringVector &args)
 {
+	MasterIndexList masterIndices;
     SlaveList slaves;
     SlaveList::const_iterator si;
     bool showHeader, multiMaster;
@@ -99,10 +100,11 @@ void CommandSdos::execute(const StringVector &args)
         throwInvalidUsageException(err);
     }
 
-    multiMaster = getMasterIndices().size() > 1;
+	masterIndices = getMasterIndices();
+    multiMaster = masterIndices.size() > 1;
     MasterIndexList::const_iterator mi;
-    for (mi = getMasterIndices().begin();
-            mi != getMasterIndices().end(); mi++) {
+    for (mi = masterIndices.begin();
+            mi != masterIndices.end(); mi++) {
         MasterDevice m(*mi);
         m.open(MasterDevice::Read);
         slaves = selectedSlaves(m);
diff --git a/tool/CommandSiiRead.cpp b/tool/CommandSiiRead.cpp
index 611d6558..368b2e59 100644
--- a/tool/CommandSiiRead.cpp
+++ b/tool/CommandSiiRead.cpp
@@ -76,6 +76,7 @@ string CommandSiiRead::helpString() const
 
 void CommandSiiRead::execute(const StringVector &args)
 {
+	MasterIndexList masterIndices;
     SlaveList slaves;
     ec_ioctl_slave_t *slave;
     ec_ioctl_slave_sii_t data;
@@ -89,11 +90,12 @@ void CommandSiiRead::execute(const StringVector &args)
         throwInvalidUsageException(err);
     }
 
-    if (getMasterIndices().size() != 1) {
+	masterIndices = getMasterIndices();
+    if (masterIndices.size() != 1) {
         err << getName() << " requires to select a single master!";
         throwInvalidUsageException(err);
     }
-    MasterDevice m(getMasterIndices().front());
+    MasterDevice m(masterIndices.front());
     m.open(MasterDevice::Read);
     slaves = selectedSlaves(m);
 
diff --git a/tool/CommandSiiWrite.cpp b/tool/CommandSiiWrite.cpp
index 2f3a2dd1..4aa3eddd 100644
--- a/tool/CommandSiiWrite.cpp
+++ b/tool/CommandSiiWrite.cpp
@@ -78,6 +78,7 @@ string CommandSiiWrite::helpString() const
 
 void CommandSiiWrite::execute(const StringVector &args)
 {
+	MasterIndexList masterIndices;
     stringstream err;
     ec_ioctl_slave_sii_t data;
     ifstream file;
@@ -88,11 +89,12 @@ void CommandSiiWrite::execute(const StringVector &args)
         throwInvalidUsageException(err);
     }
 
-    if (getMasterIndices().size() != 1) {
+	masterIndices = getMasterIndices();
+    if (masterIndices.size() != 1) {
         err << getName() << " requires to select a single master!";
         throwInvalidUsageException(err);
     }
-    MasterDevice m(getMasterIndices().front());
+    MasterDevice m(masterIndices.front());
 
     if (args[0] == "-") {
         loadSiiData(&data, cin);
diff --git a/tool/CommandSlaves.cpp b/tool/CommandSlaves.cpp
index 8e4f6bc3..fe8cb516 100644
--- a/tool/CommandSlaves.cpp
+++ b/tool/CommandSlaves.cpp
@@ -108,6 +108,7 @@ string CommandSlaves::helpString() const
 
 void CommandSlaves::execute(const StringVector &args)
 {
+	MasterIndexList masterIndices;
     SlaveList slaves;
     bool doIndent;
     
@@ -117,10 +118,11 @@ void CommandSlaves::execute(const StringVector &args)
         throwInvalidUsageException(err);
     }
 
-    doIndent = getMasterIndices().size() > 1;
+	masterIndices = getMasterIndices();
+    doIndent = masterIndices.size() > 1;
     MasterIndexList::const_iterator mi;
-    for (mi = getMasterIndices().begin();
-            mi != getMasterIndices().end(); mi++) {
+    for (mi = masterIndices.begin();
+            mi != masterIndices.end(); mi++) {
         MasterDevice m(*mi);
         m.open(MasterDevice::Read);
         slaves = selectedSlaves(m);
diff --git a/tool/CommandSoeRead.cpp b/tool/CommandSoeRead.cpp
index 6edb894d..d46999a0 100644
--- a/tool/CommandSoeRead.cpp
+++ b/tool/CommandSoeRead.cpp
@@ -78,6 +78,7 @@ string CommandSoeRead::helpString() const
 
 void CommandSoeRead::execute(const StringVector &args)
 {
+	MasterIndexList masterIndices;
     SlaveList slaves;
     stringstream err;
     const DataType *dataType = NULL;
@@ -95,11 +96,12 @@ void CommandSoeRead::execute(const StringVector &args)
         throwInvalidUsageException(err);
     }
 
-    if (getMasterIndices().size() != 1) {
+	masterIndices = getMasterIndices();
+    if (masterIndices.size() != 1) {
         err << getName() << " requires to select a single master!";
         throwInvalidUsageException(err);
     }
-    MasterDevice m(getMasterIndices().front());
+    MasterDevice m(masterIndices.front());
     m.open(MasterDevice::Read);
     slaves = selectedSlaves(m);
     if (slaves.size() != 1) {
diff --git a/tool/CommandSoeWrite.cpp b/tool/CommandSoeWrite.cpp
index a67345b4..6c348521 100644
--- a/tool/CommandSoeWrite.cpp
+++ b/tool/CommandSoeWrite.cpp
@@ -80,6 +80,7 @@ string CommandSoeWrite::helpString() const
 
 void CommandSoeWrite::execute(const StringVector &args)
 {
+	MasterIndexList masterIndices;
     stringstream strIdn, err;
     const DataType *dataType = NULL;
     ec_ioctl_slave_soe_write_t ioctl;
@@ -98,11 +99,12 @@ void CommandSoeWrite::execute(const StringVector &args)
         throwInvalidUsageException(err);
     }
 
-    if (getMasterIndices().size() != 1) {
+	masterIndices = getMasterIndices();
+    if (masterIndices.size() != 1) {
         err << getName() << " requires to select a single master!";
         throwInvalidUsageException(err);
     }
-    MasterDevice m(getMasterIndices().front());
+    MasterDevice m(masterIndices.front());
     m.open(MasterDevice::ReadWrite);
     slaves = selectedSlaves(m);
     if (slaves.size() != 1) {
diff --git a/tool/CommandStates.cpp b/tool/CommandStates.cpp
index 24f248ba..663979c8 100644
--- a/tool/CommandStates.cpp
+++ b/tool/CommandStates.cpp
@@ -68,6 +68,7 @@ string CommandStates::helpString() const
 
 void CommandStates::execute(const StringVector &args)
 {
+	MasterIndexList masterIndices;
     SlaveList slaves;
     SlaveList::const_iterator si;
     stringstream err;
@@ -98,9 +99,10 @@ void CommandStates::execute(const StringVector &args)
         throwInvalidUsageException(err);
     }
 
+	masterIndices = getMasterIndices();
     MasterIndexList::const_iterator mi;
-    for (mi = getMasterIndices().begin();
-            mi != getMasterIndices().end(); mi++) {
+    for (mi = masterIndices.begin();
+            mi != masterIndices.end(); mi++) {
         MasterDevice m(*mi);
         m.open(MasterDevice::ReadWrite);
         slaves = selectedSlaves(m);
diff --git a/tool/CommandUpload.cpp b/tool/CommandUpload.cpp
index 7a186f54..38d3fbf7 100644
--- a/tool/CommandUpload.cpp
+++ b/tool/CommandUpload.cpp
@@ -82,6 +82,7 @@ string CommandUpload::helpString() const
 
 void CommandUpload::execute(const StringVector &args)
 {
+	MasterIndexList masterIndices;
     SlaveList slaves;
     stringstream err, strIndex, strSubIndex;
     ec_ioctl_slave_sdo_upload_t data;
@@ -112,11 +113,12 @@ void CommandUpload::execute(const StringVector &args)
     }
     data.sdo_entry_subindex = uval;
 
-    if (getMasterIndices().size() != 1) {
+	masterIndices = getMasterIndices();
+    if (masterIndices.size() != 1) {
         err << getName() << " requires to select a single master!";
         throwInvalidUsageException(err);
     }
-    MasterDevice m(getMasterIndices().front());
+    MasterDevice m(masterIndices.front());
     m.open(MasterDevice::Read);
     slaves = selectedSlaves(m);
     if (slaves.size() != 1) {
diff --git a/tool/CommandXml.cpp b/tool/CommandXml.cpp
index 8682fe57..4969fa59 100644
--- a/tool/CommandXml.cpp
+++ b/tool/CommandXml.cpp
@@ -71,6 +71,7 @@ string CommandXml::helpString() const
 
 void CommandXml::execute(const StringVector &args)
 {
+	MasterIndexList masterIndices;
     SlaveList slaves;
     SlaveList::const_iterator si;
 
@@ -80,12 +81,13 @@ void CommandXml::execute(const StringVector &args)
         throwInvalidUsageException(err);
     }
 
-    if (getMasterIndices().size() != 1) {
+	masterIndices = getMasterIndices();
+    if (masterIndices.size() != 1) {
         stringstream err;
         err << getName() << " requires to select a single master!";
         throwInvalidUsageException(err);
     }
-    MasterDevice m(getMasterIndices().front());
+    MasterDevice m(masterIndices.front());
     m.open(MasterDevice::Read);
     slaves = selectedSlaves(m);
 
diff --git a/tool/main.cpp b/tool/main.cpp
index 7ccce7b1..c823fcad 100644
--- a/tool/main.cpp
+++ b/tool/main.cpp
@@ -63,7 +63,6 @@ using namespace std;
 #include "CommandVersion.h"
 #include "CommandXml.h"
 
-#include "NumberListParser.h"
 #include "MasterDevice.h"
 
 /*****************************************************************************/
@@ -76,8 +75,7 @@ string commandName;
 Command::StringVector commandArgs;
 
 // option variables
-list<unsigned int> masterIndices;
-string masterIndexList = "-"; // all masters
+string masters = "-"; // all masters
 int slavePosition = -1;
 int slaveAlias = -1;
 int domainIndex = -1;
@@ -113,7 +111,10 @@ string usage()
 
     str << endl
         << "Global options:" << endl
-        << "  --master  -m <master>  Index of the master to use. Default: 0."
+        << "  --master  -m <master>  Comma separated list of masters" << endl
+        << "                         to select, ranges are allowed." << endl
+        << "                         Examples: '1,3', '5-7,9', '-3'." << endl
+        << "                         Default: '-' (all)."
         << endl
         << "  --force   -f           Force a command." << endl
         << "  --quiet   -q           Output less information." << endl
@@ -132,20 +133,6 @@ string usage()
 
 /*****************************************************************************/
 
-class MasterIndexParser:
-    public NumberListParser
-{
-    unsigned int getMax()
-    {
-        MasterDevice dev;
-        dev.setIndex(0U);
-        dev.open(MasterDevice::Read);
-        return dev.getMasterCount() - 1;
-    };
-};
-
-/*****************************************************************************/
-
 void getOptions(int argc, char **argv)
 {
     int c, argCount;
@@ -171,7 +158,7 @@ void getOptions(int argc, char **argv)
 
         switch (c) {
             case 'm':
-                masterIndexList = optarg;
+                masters = optarg;
                 break;
 
             case 'a':
@@ -261,19 +248,6 @@ void getOptions(int argc, char **argv)
         }
     }
 
-    try {
-        MasterIndexParser p;
-        masterIndices = p.parse(masterIndexList.c_str());
-    } catch (MasterDeviceException &e) {
-        cerr << "Failed to obtain number of masters: " << e.what() << endl;
-        exit(1);
-    } catch (runtime_error &e) {
-        cerr << "Invalid master argument " << masterIndexList
-            << ": " << e.what() << endl
-            << endl << usage();
-        exit(1);
-    }
-
     commandName = argv[optind];
     while (++optind < argc)
         commandArgs.push_back(string(argv[optind]));
@@ -348,18 +322,12 @@ int main(int argc, char **argv)
 
     matchingCommands = getMatchingCommands(commandName);
 
-    if (masterIndices.empty()) {
-        cerr << "List of master indices may not be empty!" << endl
-            << endl << usage();
-        exit(1);
-    }
-
     if (matchingCommands.size()) {
         if (matchingCommands.size() == 1) {
             cmd = matchingCommands.front();
             if (!helpRequested) {
                 try {
-                    cmd->setMasterIndices(masterIndices);
+                    cmd->setMasters(masters);
                     cmd->setVerbosity(verbosity);
                     cmd->setAlias(slaveAlias);
                     cmd->setPosition(slavePosition);
-- 
GitLab