From 34976c76021748db7fdfa30ca7d787680e0a6289 Mon Sep 17 00:00:00 2001
From: Antonio Quartulli <antonio@mandelbit.com>
Date: Mon, 29 Jan 2018 16:05:11 +0800
Subject: [PATCH] DaemonCLI: add --conf flag

When starting a daemon with a different configuration,
the same should also be used by the cli tool.

Add the --conf flag to the cli command to allow using a
custom config file.

Signed-off-by: Antonio Quartulli <antonio@mandelbit.com>
---
 CHANGELOG.md                |  1 +
 lbrynet/daemon/DaemonCLI.py | 29 ++++++++++++++++++++++-------
 2 files changed, 23 insertions(+), 7 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 54e777a2a..e8b1ed089 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -56,6 +56,7 @@ at anytime.
   * Added abandon information (claim name, id, address, amount, balance_delta and nout) about claims, supports, and updates to `transaction_list` results under `abandon_info` key
   * Added `permanent_url` attribute to `channel_list_mine`, `claim_list`, `claim_show`, `resolve` and `resolve_name` API calls through lbryio/lbryum#203
   *
+  * Added `--conf` CLI flag to lbrynet-cli tool to specify an alternative config file
 
 ### Changed
   * claim_show API command no longer takes name as argument
diff --git a/lbrynet/daemon/DaemonCLI.py b/lbrynet/daemon/DaemonCLI.py
index fe16806fa..af954dbb0 100644
--- a/lbrynet/daemon/DaemonCLI.py
+++ b/lbrynet/daemon/DaemonCLI.py
@@ -38,8 +38,22 @@ def set_flag_vals(flag_names, parsed_args):
 
 
 def main():
-    if len(sys.argv[1:]):
-        method, args = sys.argv[1], sys.argv[2:]
+    argv = sys.argv[1:]
+
+    # check if a config file has been specified. If so, shift
+    # all the arguments so that the parsing can continue without
+    # noticing
+    if len(argv) and argv[0] == "--conf":
+        if len(argv) < 2:
+            print_error("No config file specified for --conf option")
+            print_help()
+            return
+
+        conf.conf_file = argv[1]
+        argv = argv[2:]
+
+    if len(argv):
+        method, args = argv[0], argv[1:]
     else:
         print_help()
         return
@@ -176,13 +190,14 @@ def print_help():
         "   lbrynet-cli - LBRY command line client.",
         "",
         "USAGE",
-        "   lbrynet-cli <command> [<args>]",
+        "   lbrynet-cli [--conf <config file>] <command> [<args>]",
         "",
         "EXAMPLES",
-        "   lbrynet-cli commands            # list available commands",
-        "   lbrynet-cli status              # get daemon status",
-        "   lbrynet-cli resolve_name what   # resolve a name",
-        "   lbrynet-cli help resolve_name   # get help for a command",
+        "   lbrynet-cli commands                 # list available commands",
+        "   lbrynet-cli status                   # get daemon status",
+        "   lbrynet-cli --conf ~/l1.conf status  # like above but using ~/l1.conf as config file",
+        "   lbrynet-cli resolve_name what        # resolve a name",
+        "   lbrynet-cli help resolve_name        # get help for a command",
     ])