# -*- coding: utf-8 -*-

# PyAX-12

# The MIT License
#
# Copyright (c) 2010,2015 Jeremie DECOCK (http://www.jdhp.org)
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

"""
Common functions for PyAX-12 demos.
"""

__all__ = ['common_argument_parser']

import argparse

import pyax12.packet as pk


ID_HELP_STR = ("The unique ID of a Dynamixel unit to work with "
               "(254 is a broadcasting ID)")

BAUDRATE_HELP_STR = "The baudrate speed (e.g. 57600)"

TIMEOUT_HELP_STR = "The timeout value for the connection"

PORT_HELP_STR = ("The serial device to connect with "
                 "(e.g. '/dev/ttyUSB0' for Unix users "
                 "or 'COM1' for Windows users)")


def common_argument_parser(desc, id_arg=True):
    """Return a preconfigured `argparse` parser instance.

    :param str desc: the global description of the program (printed with -h or
        --help).
    :param bool id_arg: argparse ignores the `dynamixel_id` option if `id_arg`
        is False (`dynamixel_id` is not relevant for some programs e.g.
        examples/scan.py).
    """

    # Parse options
    parser = argparse.ArgumentParser(description=desc)

    if id_arg:
        parser.add_argument("--dynamixel_id",
                            "-i",
                            help=ID_HELP_STR,
                            metavar="INTEGER",
                            type=int,
                            default=pk.BROADCAST_ID)

    parser.add_argument("--baudrate",
                        "-b",
                        help=BAUDRATE_HELP_STR,
                        metavar="INTEGER",
                        type=int,
                        default=57600)

    parser.add_argument("--timeout",
                        "-t",
                        help=TIMEOUT_HELP_STR,
                        metavar="FLOAT",
                        type=float,
                        default=0.1)

    parser.add_argument("--port",
                        "-p",
                        help=PORT_HELP_STR,
                        metavar="STRING",
                        default="/dev/ttyUSB0")

    return parser


@@ -1729,16 +1729,40 @@
 arg=True
+, id_arg_mandatory=False
 ):%0A    %22
@@ -1935,24 +1935,25 @@
 id_arg: 
+%60
 argparse
  ignores
@@ -1944,16 +1944,17 @@
 argparse
+%60
  ignores
@@ -1982,16 +1982,24 @@
 ption if
+%0A       
  %60id_arg
@@ -2003,25 +2003,21 @@
 arg%60
-%0A       
  is 
+%60%60
 False
+%60%60
  (%60d
@@ -2066,21 +2066,16 @@
 rams
- e.g.
 %0A       
  exa
@@ -2070,16 +2070,21 @@
 %0A       
+ e.g.
  example
@@ -2095,16 +2095,188 @@
 an.py).%0A
+    :param bool id_arg_mandatory: the %60dynamixel_id%60 option is mandatory if%0A        this parameter is %60%60True%60%60. This parameter is ignored if %60id_arg%60 is%0A        %60%60False%60%60.%0A
     %22%22%22%0A
@@ -2367,16 +2367,49 @@
 id_arg:%0A
+        if id_arg_mandatory:%0A    
         
@@ -2478,14 +2478,22 @@
     
+ 
+   
 %22-i%22,%0A
+    
     
@@ -2554,32 +2554,36 @@
                 
+  
+  
 metavar=%22INTEGER
@@ -2605,32 +2605,36 @@
                 
+    
 type=int,%0A      
@@ -2619,32 +2619,385 @@
       type=int,%0A
+                                required=True,%0A                                default=pk.BROADCAST_ID)%0A        else:%0A            parser.add_argument(%22--dynamixel_id%22,%0A                                %22-i%22,%0A                                help=ID_HELP_STR,%0A                                metavar=%22INTEGER%22,%0A                                type=int,%0A    
                 

Add the 'id_arg_mandatory' parameter.