#!/bin/python3

import json
import sys
from argparse import ArgumentParser

from couchbase.bucket import Bucket
from couchbase.exceptions import BucketNotFoundError
from couchbase.exceptions import CouchbaseNetworkError
from couchbase.exceptions import NotFoundError


def get_document_catch_error(bucket, doc):
    try:
        return bucket.get(doc).value
    except NotFoundError:
        sys.exit("Error: The given document ID " + doc + " is invalid.")


def get(bucket, doc):
    document = get_document_catch_error(bucket, doc)
    print(json.dumps(document, indent=2, sort_keys=True))


def replace(bucket, doc, set_json_key, set_json_value):
    if set_json_key is None:
        sys.exit("Error: A REPLACE operation requires a key to set (use the flag -sk KEY).")
    elif set_json_value is None:
        sys.exit("Error: A REPLACE operation requires a value to set (use the flag -sv VALUE).")

    document = get_document_catch_error(bucket, doc)

    iterator = document
    json_hierarchy = set_json_key.split(".")
    levels = len(json_hierarchy)
    for i in range(levels-1):
        iterator = iterator[json_hierarchy[i]]
    iterator[json_hierarchy[levels-1]] = set_json_value

    bucket.replace(doc, document)
    print("REPLACE operation was successful.")


def main():
    url = "http://localhost:8091"

    parser = ArgumentParser(description="Utility tool for interacting with Couchbase from the command line.")
    parser.add_argument("operation", type=str, choices=["GET", "REPLACE"], help="Operation to perform on Couchbase")
    parser.add_argument("bucket", type=str, help="Name of the bucket")
    parser.add_argument("document", type=str, help="Unique ID of the Document")
    parser.add_argument("-sk", "--set_key", nargs="?", type=str, help="(REPLACE only) Key to replace")
    parser.add_argument(
            "-sv", "--set_value", nargs="?", type=str,
            help="(REPLACE only) Value to assign to the replaced key"
    )
    args = parser.parse_args()

    bucket = None
    try:
        bucket = Bucket(url + "/" + args.bucket)  # Instantiate bucket connection
    except CouchbaseNetworkError:
        sys.exit("Error: Could not connect to Couchbase at " + url + ".")
    except BucketNotFoundError:
        sys.exit("Error: The given bucket name " + args.bucket + " is invalid.")

    if args.operation == "GET":
        get(bucket, args.document)
    elif args.operation == "REPLACE":
        replace(bucket, args.document, args.set_key, args.set_value)
    else:
        print("Error: Operation not supported.")

if __name__ == "__main__":
    main()

@@ -402,25 +402,18 @@
 %22Error: 
-The given
+No
  documen
@@ -414,16 +414,21 @@
 ocument 
+with 
 ID %22 + d
@@ -430,34 +430,30 @@
 %22 + doc + %22 
-is invalid
+exists
 .%22)%0A%0A%0Adef ge
@@ -1258,24 +1258,178 @@
 cessful.%22)%0A%0A
+def delete(bucket, doc):%0A    try:%0A        bucket.remove(doc)%0A    except NotFoundError:%0A        sys.exit(%22Error: No document with ID %22 + doc + %22 exists.%22)%0A
 %0Adef main():
@@ -1646,16 +1646,26 @@
 REPLACE%22
+, %22DELETE%22
 %5D, help=
@@ -2579,16 +2579,16 @@
 PLACE%22:%0A
-
         
@@ -2648,16 +2648,91 @@
 _value)%0A
+    elif args.operation == %22DELETE%22:%0A        delete(bucket, args.document)%0A
     else

Add support for DELETE operation