#!/usr/bin/env python3
# Copyright (c) 2015-2019 The Syscoin Core developers
# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.

'''
This checks if all command line args are documented.
Return value is 0 to indicate no error.

Author: @MarcoFalke
'''

from subprocess import check_output
import re
import sys

FOLDER_GREP = 'src'
FOLDER_TEST = 'src/test/'
REGEX_ARG = '(?:ForceSet|SoftSet|Get|Is)(?:Bool)?Args?(?:Set)?\("(-[^"]+)"'
REGEX_DOC = 'AddArg\("(-[^"=]+?)(?:=|")'
CMD_ROOT_DIR = '`git rev-parse --show-toplevel`/{}'.format(FOLDER_GREP)
CMD_GREP_ARGS = r"git grep --perl-regexp '{}' -- {} ':(exclude){}'".format(REGEX_ARG, CMD_ROOT_DIR, FOLDER_TEST)
CMD_GREP_DOCS = r"git grep --perl-regexp '{}' {}".format(REGEX_DOC, CMD_ROOT_DIR)
# list unsupported, deprecated and duplicate args as they need no documentation
SET_DOC_OPTIONAL = set(['-h', '-help', '-dbcrashratio', '-forcecompactdb'])


def main():
    if sys.version_info >= (3, 6):
        used = check_output(CMD_GREP_ARGS, shell=True, universal_newlines=True, encoding='utf8')
        docd = check_output(CMD_GREP_DOCS, shell=True, universal_newlines=True, encoding='utf8')
    else:
        used = check_output(CMD_GREP_ARGS, shell=True).decode('utf8').strip()
        docd = check_output(CMD_GREP_DOCS, shell=True).decode('utf8').strip()

    args_used = set(re.findall(re.compile(REGEX_ARG), used))
    args_docd = set(re.findall(re.compile(REGEX_DOC), docd)).union(SET_DOC_OPTIONAL)
    args_need_doc = args_used.difference(args_docd)
    args_unknown = args_docd.difference(args_used)

    print("Args used        : {}".format(len(args_used)))
    print("Args documented  : {}".format(len(args_docd)))
    print("Args undocumented: {}".format(len(args_need_doc)))
    print(args_need_doc)
    print("Args unknown     : {}".format(len(args_unknown)))
    print(args_unknown)

    sys.exit(len(args_need_doc))


if __name__ == "__main__":
    main()

@@ -380,19 +380,8 @@
 t re
-%0Aimport sys
 %0A%0AFO
@@ -557,17 +557,18 @@
 _DIR = '
-%60
+$(
 git rev-
@@ -592,9 +592,9 @@
 evel
-%60
+)
 /%7B%7D'
@@ -740,494 +740,542 @@
 REP_
-DOCS = r%22git grep --perl-regexp '%7B%7D' %7B%7D%22.format(REGEX_DOC, CMD_ROOT_DIR)%0A# list unsupported, deprecated and duplicate args as they need no documentation%0ASET_DOC_OPTIONAL = set(%5B'-h', '-help', '-dbcrashratio', '-forcecompactdb'%5D)%0A%0A%0Adef main():%0A    if sys.version_info %3E= (3, 6):%0A        used = check_output(CMD_GREP_ARGS, shell=True, universal_newlines=True, encoding='utf8')%0A        docd = check_output(CMD_GREP_DOCS, shell=True, universal_newlines=True, encoding='utf8')%0A    else:%0A    
+WALLET_ARGS = r%22git grep --function-context 'void WalletInit::AddWalletOptions' -- %7B%7D %7C grep AddArg%22.format(CMD_ROOT_DIR)%0ACMD_GREP_WALLET_HIDDEN_ARGS = r%22git grep --function-context 'void DummyWalletInit::AddWalletOptions' -- %7B%7D%22.format(CMD_ROOT_DIR)%0ACMD_GREP_DOCS = r%22git grep --perl-regexp '%7B%7D' %7B%7D%22.format(REGEX_DOC, CMD_ROOT_DIR)%0A# list unsupported, deprecated and duplicate args as they need no documentation%0ASET_DOC_OPTIONAL = set(%5B'-h', '-help', '-dbcrashratio', '-forcecompactdb'%5D)%0A%0A%0Adef lint_missing_argument_documentation():%0A
     
@@ -1344,20 +1344,16 @@
 strip()%0A
-    
     docd
@@ -1966,35 +1966,794 @@
     
-sys.exit(len(args_need_doc)
+assert 0 == len(args_need_doc), %22Please document the following arguments: %7B%7D%22.format(args_need_doc)%0A%0A%0Adef lint_missing_hidden_wallet_args():%0A    wallet_args = check_output(CMD_GREP_WALLET_ARGS, shell=True).decode('utf8').strip()%0A    wallet_hidden_args = check_output(CMD_GREP_WALLET_HIDDEN_ARGS, shell=True).decode('utf8').strip()%0A%0A    wallet_args = set(re.findall(re.compile(REGEX_DOC), wallet_args))%0A    wallet_hidden_args = set(re.findall(re.compile(r'    %22(%5B%5E%22=%5D+)'), wallet_hidden_args))%0A%0A    hidden_missing = wallet_args.difference(wallet_hidden_args)%0A    if hidden_missing:%0A        assert 0, %22Please add %7B%7D to the hidden args in DummyWalletInit::AddWalletOptions%22.format(hidden_missing)%0A%0A%0Adef main():%0A    lint_missing_argument_documentation()%0A    lint_missing_hidden_wallet_args(
 )%0A%0A%0A

Check that all wallet args are hidden