     }
 }
 
+func addChapter(argc: Int, arguments: [String]) throws {
+    if argc == 2 {
+        do {
+            var book = try loadBook()
+            let chapter = Chapter(name: arguments[1])
+            try book.add(chapter: chapter)
+            try book.writeManifest()
+            try chapter.writeManifest()
+        } catch {
+            throw error
+        }
+    } else {
+        throw NSError.error(description: "Arguments is less.")
+    }
+}
+
+func addPage(argc: Int, arguments: [String]) throws {
+    if argc == 3 {
+        do {
+            let book = try loadBook()
+            var chapter = try book.getChapter(name: arguments[1])
+            let page = Page(name: arguments[2], chapterName: chapter.name)
+            try chapter.add(page: page)
+            try page.writeManifest()
+        } catch {
+            throw error
+        }
+    } else {
+        throw NSError.error(description: "Arguments is less.")
+    }
+}
+
 func parseArguments() throws {
     var arguments :[String] = ProcessInfo.processInfo().arguments
+    print(arguments.first)
     let argc = arguments.count - 1
     if argc >= 1 {
-        print(arguments.first)
         arguments.removeFirst() // remove own path
         switch arguments[0] {
         case "create":
             do { try createBook(argc: argc, arguments: arguments) } catch { throw error }
         case "chapter":
-            // add chapter to a book
-            print(argc)
+            do { try addChapter(argc: argc, arguments: arguments) } catch { throw error }
         case "page":
-            // add page to a chapter
-            print(argc)
+            do { try addPage(argc: argc, arguments: arguments) } catch { throw error }
         case "rm":
-            // remove chapter from book
-            // remove page from chapter
             print(argc)
         default:
             print(argc)

    } catch {
        print(error)
    }

//    let book = Book(name: "hoge", version: "1.0", contentIdentifier: "com.sonson.hoge", contentVersion: "1.0", deploymentTarget: "ios10.10")
//
//
//    print(arguments)
//
//    let dict = [
//        "Version" : "1.0",
//        "ContentIdentifier" : "com.sonson.hoge",
//        "Chapters" : ["a", "b"]
//    ]
//
//    let a: NSDictionary = dict as NSDictionary
//    a.write(toFile: "/Users/sonson/Desktop/hoge.plist", atomically: false)
}

main()

        }
    }
    
    func getChapter(name: String) throws -> Chapter {
        for i in 0..<chapters.count {
            if chapters[i].name == name {
                return chapters[i]
            }
        }
        throw NSError.error(description: "Not found")
    }
    
    mutating func add(chapter: Chapter) throws {
        chapters.append(chapter)
        do {
            try writeManifest()
        } catch {
            throw error
        }
    }
    
